我在浏览嵌套列表时遇到了麻烦。以下是列表中一个元素的样本结构:
$ F01 :List of 7
..$ 0:'data.frame': 16 obs. of 3 variables:
.. ..$ lengths: Factor w/ 8 levels "1","2","4","5",..: 1 2 3 4 5 6 7 8 1 2 ...
.. ..$ values : Factor w/ 2 levels "C","N": 1 1 1 1 1 1 1 1 2 2 ...
.. ..$ Freq : int [1:16] 1 2 0 1 1 1 1 0 1 3 ...
..$ 1:'data.frame': 20 obs. of 3 variables:
.. ..$ lengths: Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
.. ..$ values : Factor w/ 2 levels "C","N": 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ Freq : int [1:20] 0 1 1 1 1 0 1 0 1 1 ...
我可以使用lapply
轻松将函数应用于列表的一个元素:比如F01
lapply(data$F01,function(x) x[which(x[['values']]=="C"),])
然后我想用rapply
将其应用于整个嵌套列表:
rapply(data,function(x) x[which(x[['values']]=="C"),],how="list")
Error in `[[.default`(x, "values") : subscript out of bounds
我不明白为什么我会得到这个rapply错误,因为rapply应该递归地递归到非list元素,在这种情况下是data.frame。是否有一些我不明白的东西?
这是主列表中两个完整元素的示例:
samp <- list(structure(list(`0` = structure(list(lengths = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("1",
"2", "7", "8", "13", "18"), class = "factor"), values = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("C",
"N"), class = "factor"), Freq = c(0L, 1L, 1L, 1L, 1L, 0L, 2L,
0L, 0L, 0L, 0L, 1L)), .Names = c("lengths", "values", "Freq"), row.names = c(NA,
-12L), class = "data.frame"), `1` = structure(list(lengths = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("1",
"2", "3", "5", "8", "12"), class = "factor"), values = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("C",
"N"), class = "factor"), Freq = c(1L, 1L, 0L, 1L, 1L, 1L, 2L,
0L, 1L, 1L, 0L, 0L)), .Names = c("lengths", "values", "Freq"), row.names = c(NA,
-12L), class = "data.frame"), `2` = structure(list(lengths = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("1",
"3", "4", "6", "9", "19", "20"), class = "factor"), values = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("C",
"N"), class = "factor"), Freq = c(1L, 1L, 1L, 1L, 0L, 1L, 0L,
0L, 0L, 3L, 0L, 1L, 0L, 2L)), .Names = c("lengths", "values",
"Freq"), row.names = c(NA, -14L), class = "data.frame"), `3` = structure(list(
lengths = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L), .Label = c("1", "2", "3", "4",
"5", "8", "11", "18"), class = "factor"), values = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("C", "N"), class = "factor"), Freq = c(1L,
2L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 2L, 1L, 1L, 1L, 0L, 0L, 1L
)), .Names = c("lengths", "values", "Freq"), row.names = c(NA,
-16L), class = "data.frame"), `4` = structure(list(lengths = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("1",
"2", "3", "4", "6", "11", "13"), class = "factor"), values = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("C",
"N"), class = "factor"), Freq = c(0L, 2L, 0L, 1L, 1L, 0L, 2L,
1L, 2L, 2L, 0L, 0L, 1L, 0L)), .Names = c("lengths", "values",
"Freq"), row.names = c(NA, -14L), class = "data.frame"), `5` = structure(list(
lengths = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), .Label = c("1", "2",
"4", "5", "6", "7", "8", "11", "23"), class = "factor"),
values = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("C", "N"), class = "factor"),
Freq = c(0L, 3L, 1L, 2L, 0L, 1L, 0L, 0L, 1L, 3L, 2L, 0L,
0L, 1L, 0L, 1L, 1L, 0L)), .Names = c("lengths", "values",
"Freq"), row.names = c(NA, -18L), class = "data.frame"), `6` = structure(list(
lengths = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .Label = c("1",
"2", "3", "4", "5", "6", "9", "12", "13", "21", "36"), class = "factor"),
values = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("C",
"N"), class = "factor"), Freq = c(2L, 2L, 3L, 1L, 2L, 1L,
2L, 1L, 0L, 0L, 0L, 2L, 3L, 1L, 4L, 0L, 1L, 0L, 0L, 1L, 1L,
1L)), .Names = c("lengths", "values", "Freq"), row.names = c(NA,
-22L), class = "data.frame")), .Names = c("0", "1", "2", "3",
"4", "5", "6")), structure(list(`0` = structure(list(lengths = structure(c(1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("2", "13", "17", "25"
), class = "factor"), values = structure(c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L), .Label = c("C", "N"), class = "factor"), Freq = c(1L,
1L, 0L, 1L, 0L, 0L, 1L, 1L)), .Names = c("lengths", "values",
"Freq"), row.names = c(NA, -8L), class = "data.frame"), `1` = structure(list(
lengths = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L,
4L, 5L, 6L), .Label = c("1", "2", "3", "4", "5", "8"), class = "factor"),
values = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("C", "N"), class = "factor"), Freq = c(0L,
0L, 1L, 2L, 2L, 0L, 1L, 1L, 0L, 1L, 1L, 1L)), .Names = c("lengths",
"values", "Freq"), row.names = c(NA, -12L), class = "data.frame"),
`2` = structure(list(lengths = structure(c(1L, 2L, 3L, 4L,
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("2",
"3", "4", "7", "14", "18", "19"), class = "factor"), values = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("C",
"N"), class = "factor"), Freq = c(1L, 1L, 2L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L)), .Names = c("lengths", "values",
"Freq"), row.names = c(NA, -14L), class = "data.frame"),
`3` = structure(list(lengths = structure(c(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), .Label = c("2",
"3", "5", "8", "9", "10", "19", "76"), class = "factor"),
values = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("C", "N"), class = "factor"),
Freq = c(1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L,
1L, 1L, 0L, 1L, 1L)), .Names = c("lengths", "values",
"Freq"), row.names = c(NA, -16L), class = "data.frame"),
`4` = structure(list(lengths = structure(c(1L, 2L, 3L, 4L,
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("2",
"5", "7", "8", "9", "16", "35"), class = "factor"), values = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("C",
"N"), class = "factor"), Freq = c(1L, 1L, 2L, 0L, 1L, 0L,
0L, 1L, 0L, 0L, 2L, 0L, 1L, 1L)), .Names = c("lengths", "values",
"Freq"), row.names = c(NA, -14L), class = "data.frame"),
`5` = structure(list(lengths = structure(c(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), .Label = c("1",
"2", "3", "5", "6", "10", "11", "14", "27"), class = "factor"),
values = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("C",
"N"), class = "factor"), Freq = c(2L, 2L, 1L, 1L, 1L,
1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L)), .Names = c("lengths",
"values", "Freq"), row.names = c(NA, -18L), class = "data.frame"),
`6` = structure(list(lengths = structure(c(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), .Label = c("1",
"2", "3", "4", "5", "6", "11", "21", "51"), class = "factor"),
values = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("C",
"N"), class = "factor"), Freq = c(2L, 1L, 2L, 2L, 1L,
1L, 0L, 0L, 0L, 3L, 0L, 2L, 0L, 1L, 1L, 1L, 1L, 1L)), .Names = c("lengths",
"values", "Freq"), row.names = c(NA, -18L), class = "data.frame")), .Names = c("0",
"1", "2", "3", "4", "5", "6")))
答案 0 :(得分:2)
我不相信你真的想在这里使用rapply
,因为你似乎不想要总递归。也就是说,您不是要尝试将函数应用于lengths
,然后再应用于values
等。
相反,只需尝试两个嵌套的lapply
:
lapply(dat, lapply, function(x) x[which(x[['values']]=="C"),])