這是來自此one的後續問題,我想在列表中創建一個列表,現在我希望能夠訪問嵌套列表並在其中實現函數太。如何使用循環訪問列表中的所有列表
我有3個列表,其內具有兩個列表:
my_list
structure(list(a = structure(list(a = structure(list(value = c(1L,
4L), class = structure(c(1L, 1L), .Label = c("a", "b", "c"), class = "factor")), .Names = c("value",
"class"), row.names = c(1L, 4L), class = "data.frame"), b = structure(list(
value = c(2, 1), class = structure(c(2L, 2L), .Label = c("a",
"b", "c"), class = "factor")), .Names = c("value", "class"
), row.names = c(2L, 5L), class = "data.frame")), .Names = c("a",
"b")), b = structure(list(a = structure(list(value = c(6, 1),
class = structure(c(1L, 1L), .Label = c("a", "b", "c", "d"
), class = "factor")), .Names = c("value", "class"), row.names = c(1L,
5L), class = "data.frame"), b = structure(list(value = c(5, 1
), class = structure(c(2L, 2L), .Label = c("a", "b", "c", "d"
), class = "factor")), .Names = c("value", "class"), row.names = c(2L,
6L), class = "data.frame")), .Names = c("a", "b")), c = structure(list(
a = structure(list(value = 5:6, class = structure(c(1L, 1L
), .Label = c("a", "b", "c", "d", "e"), class = "factor")), .Names = c("value",
"class"), row.names = 5:6, class = "data.frame"), b = structure(list(
value = c(4, 1), class = structure(c(2L, 2L), .Label = c("a",
"b", "c", "d", "e"), class = "factor")), .Names = c("value",
"class"), row.names = c(4L, 7L), class = "data.frame")), .Names = c("a",
"b"))), .Names = c("a", "b", "c"))
$a
$a$a
value class
1 a
4 a
$a$b
value class
2 b
1 b
$b
$b$a
value class
6 a
1 a
$b$b
value class
5 b
1 b
$c
$c$a
value class
5 a
6 a
$c$b
value class
4 b
1 b
我通過value != 1
要subset
每個嵌套列表使用循環使得所得到的名單看起來是這樣的:
my_list
$a
$a$a
value class
4 a
$a$b
value class
2 b
$b
$b$a
value class
6 a
$b$b
value class
5 b
$c
$c$a
value class
5 a
6 a
$c$b
value class
4 b
有了下面的代碼,我只能訪問list [[1]][[1]]
,list[[2]][[2]]
和list[[3]][[3]]
。我如何通過循環訪問其他人並應用subset
函數?
for (i in 1:length(my_list)) {
my_list[[i]][[i]] <- subset(my_list[[i]][[i]],my_list[[i]][[i]]$value=1)
}
我注意到您沒有鏈接到您昨天就此主題提出的問題。如果兩者不同,請說明http://stackoverflow.com/questions/38954154/using-a-loop-or-vectorisation-to-subset-a-list-by-multiple-elements-in-a-vecto –
好主意,編輯好吧 – Bonono
好點 - 現在修好了 – Bonono