我无法在嵌套列表上进行快速讨论。这是列表中一个元素的样本结构:
$ 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 应该递归地应用于非列表元素,在本例中为 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")))