如何在 R 中循环列表的子集?
How to loop subset of lists in R?
我有一个包含 9 个列表的列表,请参阅以下代码,其中我只想循环三个列表 p
、r
和 t
用于 Pearson、Spearson 和 Kendall 相关性,分别,而不是所有 9 个列表。
当前伪代码如下,其中测试函数为corrplot(M.cor, ...)
,完整伪代码见下方
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个列表的列表
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 个列表
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
R: 3.3.1
OS:Debian 8.5
我的建议是在循环之前过滤列表,即
for (i in p.mat.all[c("t", "p", "r")]) {
...
}
r
corrplot、t
corrplot 和 p
corrplot
的输出
或使用 *apply 函数:
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"
)
})
我有一个包含 9 个列表的列表,请参阅以下代码,其中我只想循环三个列表 p
、r
和 t
用于 Pearson、Spearson 和 Kendall 相关性,分别,而不是所有 9 个列表。
当前伪代码如下,其中测试函数为corrplot(M.cor, ...)
,完整伪代码见下方
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个列表的列表
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 个列表
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
R: 3.3.1
OS:Debian 8.5
我的建议是在循环之前过滤列表,即
for (i in p.mat.all[c("t", "p", "r")]) {
...
}
r
corrplot、t
corrplot 和 p
corrplot
或使用 *apply 函数:
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"
)
})