如何在R中循环列表的子集? [英] How to loop subset of lists in R?
问题描述
我有一个9个列表的列表,请参见以下代码,在这里我只想循环三个列表分别用于Pearson,Spearson和Kendall相关的p
,r
和t
,而不是所有9个列表.
当前的伪代码如下,其中测试功能为corrplot(M.cor, ...)
,请参见下面的完整伪代码
I have a list of 9 lists, see the following code where I want to loop only three lists p
, r
and t
for Pearson, Spearson and Kendall correlations, respectively, instead of all 9 lists.
The current pseudocode is the following where the test function is corrplot(M.cor, ...)
, see below the complete pseudocode
for (i in p.mat.all) {
...
}
带有mtcars
测试数据的代码
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)
有关9个列表的列表的输出
Output about the 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" ...
我的有关使用测试函数corrplot
循环所有三个相关性的伪代码,但由于它遍历了所有9个列表,因此无法正常工作
My pseudocode about looping all three correlations with the test function corrplot
, but it will not work because it goes through all 9 lists
for (i in p.mat.all) {
p.mat <- i
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"
)
}
预期的输出:仅循环t
,p
和r
列表,以便可以将它们传递给测试功能corrplot
Expected output: loop only t
, p
and r
lists such that they can be passed to the test function corrplot
R:3.3.1
操作系统:Debian 8.5
R: 3.3.1
OS: Debian 8.5
推荐答案
或带有* apply函数:
Or with an *apply function:
lapply(p.mat.all[c("r","p","t")], function(x) {
# x takes now first p.mat.all$r, then 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"
)
})
这篇关于如何在R中循环列表的子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!