How to loop subset of lists in R? -


i have list of 9 lists, see following code want loop 3 lists p, r , t pearson, spearson , kendall correlations, respectively, instead of 9 lists. current pseudocode following test function corrplot(m.cor, ...), see below complete pseudocode

for (i in p.mat.all) { ... } 

code mtcars test data

library("psych") library("corrplot")     m <- mtcars   m.cor <- cor(m)  p.mat.all <- psych::corr.test(m.cor, method = c("pearson", "kendall", "spearman"),     adjust = "none", ci = f)  str(p.mat.all)  str(p.mat.all$r)  str(p.mat.all$t)  str(p.mat.all$p) 

output list of 9 lists

list of 9  $ r     : num [1:11, 1:11] 1 -0.991 -0.993 -0.956 0.939 ...   ..- attr(*, "dimnames")=list of 2   .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ...   .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ...  $ n     : num 11  $ t     : num [1:11, 1:11] inf -21.92 -25.4 -9.78 8.22 ...   ..- attr(*, "dimnames")=list of 2   .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ...   .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ...  $ p     : num [1:11, 1:11] 0.00 4.04e-09 1.09e-09 4.32e-06 1.78e-05 ...   ..- attr(*, "dimnames")=list of 2   .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ...   .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ...  $ se    : num [1:11, 1:11] 0 0.0452 0.0391 0.0978 0.1143 ...   ..- attr(*, "dimnames")=list of 2   .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ...   .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ...  $ adjust: chr "none"  $ sym   : logi true  $ ci    : null  $ call  : language psych::corr.test(x = m.cor, method = c("pearson", "kendall", "spearman"),      adjust = "none", ci = f)  - attr(*, "class")= chr [1:2] "psych" "corr.test"  num [1:11, 1:11] 1 -0.991 -0.993 -0.956 0.939 ...  - attr(*, "dimnames")=list of 2   ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ...   ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ...  num [1:11, 1:11] inf -21.92 -25.4 -9.78 8.22 ...  - attr(*, "dimnames")=list of 2   ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ...   ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ...  num [1:11, 1:11] 0.00 4.04e-09 1.09e-09 4.32e-06 1.78e-05 ...  - attr(*, "dimnames")=list of 2   ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ...   ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 

my pseudocode looping 3 correlations test function corrplot, not work because goes through 9 lists

for (i in p.mat.all) {   p.mat <-   print("p.mat ===========")   print(i)        alpha <- 0.05    corrplot( m.cor,             method="color",            type="upper",            addcoefaspercent = true,            tl.col = "black",           tl.pos = "td",            p.mat = p.mat, sig.level = alpha, insig = "blank",            order = "original"   ) } 

expected output: loop t, p , r lists such can passed test function corrplot

r: 3.3.1
os: debian 8.5

or *apply function:

lapply(p.mat.all[c("r","p","t")], function(x) {   # x takes first p.mat.all$r, p.mat.all$p, etc   print("p.mat ===========")   print(x)        alpha <- 0.05    corrplot( m.cor,             method="color",            type="upper",            addcoefaspercent = true,            tl.col = "black",           tl.pos = "td",            p.mat = x, sig.level = alpha, insig = "blank",            order = "original"   ) }) 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -