重新套用至R中的巢状清单 [英] rapply over a nested list in R

查看:75
本文介绍了重新套用至R中的巢状清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在套用嵌套列表时遇到了麻烦.这是列表中一个元素的示例结构:

I'm having trouble to rapply over a nested list. Here's the structure of a sample of one element of the list :

$ 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

I can easily apply a function to one element of the list with lapply : say F01

 lapply(data$F01,function(x) x[which(x[['values']]=="C"),])

然后我想到了要使用rapply将其应用于整个嵌套列表:

Then I thought of applying it to the whole nested list with 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.有什么明显的我不明白的地方吗?

I don't get why I get this rapply error, as rapply should lapply recursively to non list elements, in this case a data.frame. Is there something obvious that I don't get ?

这是主列表中两个完整元素的示例:

here's a sample of two complete element of the main list :

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")))

推荐答案

我不认为您实际上要在这里使用rapply,因为您似乎不希望完全递归.也就是说,您并不是要先将函数应用于lengths,然后再应用于values等.

I don't believe you actually want to use rapply here, as you do not seem to want total recursion. That is, you are not trying to apply a function to lengths and then to values, etc.

相反,只需尝试两个嵌套的lapply:

Instead, try simply two nested lapply 's:

 lapply(dat, lapply, function(x) x[which(x[['values']]=="C"),])

这篇关于重新套用至R中的巢状清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆