【问题标题】:How to run a function through multiple sublists in R?如何通过 R 中的多个子列表运行函数?
【发布时间】:2020-08-11 11:47:37
【问题描述】:

我有一个名为 AllVotes 的复杂嵌套列表,您可以在此处找到(在 .rds 中):

https://github.com/JMcrocs/MEPVote/blob/master/FinalVotes.rds

我正在尝试收集所有mepid,这是一个存储在子列表AllVotes[[x]]$votes$'+'$groups 中的命名号码。感谢之前的问题,我可以为一个列表([[1]] 列表)做到这一点,但我想对我的巢列表的所有列表从 [[1]] 到 [[2336] ].

#For the list [[1]]
Result = c(unlist( sapply ( names ( AllVotes[[1]]$votes$'+'$groups ), 
                   function(x) unlist( AllVotes[[1]]$votes$'+'$groups[[x]] ) ) ),
unlist( sapply ( names ( AllVotes[[1]]$votes$'-'$groups ), 
                   function(x) unlist( AllVotes[[1]]$votes$'-'$groups[[x]] ) )),
unlist( sapply ( names ( AllVotes[[1]]$votes$'0'$groups ), 
                   function(x) unlist( AllVotes[[1]]$votes$'0'$groups[[x]] ) )))

我该怎么做?提前谢谢你!

PS:抱歉,如果问题不完美,我对编程(和溢出)还有两周的时间。

【问题讨论】:

  • 您能否提供dput(AllVotes) 或类似结果的可重复性?
  • @niko Sadmu,dput(AllVotes) 太大,无法提供

标签: r nested-lists sapply


【解决方案1】:

您可以将代码简化为:

res <- lapply(AllVotes, function(x){
  unlist(lapply(c('+', '-', '0'), function(y){
    x$votes[[y]]$groups
  }))
})

它给出与 @Duck 的答案相同的输出。

all.equal(res, List, check.attributes = F)
# [1] TRUE

【讨论】:

    【解决方案2】:

    您可以尝试使用循环并将结果保存在列表中作为下一个:

    #Load
    AllVotes <- readRDS('FinalVotes.rds')
    #Loop
    #Empty list
    List <- list()
    #For
    for(i in 1:length(AllVotes))
    {
      List[[i]] <- c(unlist( sapply ( names ( AllVotes[[i]]$votes$'+'$groups ), 
                                      function(x) unlist( AllVotes[[i]]$votes$'+'$groups[[x]] ) ) ),
                     unlist( sapply ( names ( AllVotes[[i]]$votes$'-'$groups ), 
                                      function(x) unlist( AllVotes[[i]]$votes$'-'$groups[[x]] ) )),
                     unlist( sapply ( names ( AllVotes[[i]]$votes$'0'$groups ), 
                                      function(x) unlist( AllVotes[[i]]$votes$'0'$groups[[x]] ) )))
    }
    

    你最终会得到List 有2336 元素。您可以使用例如List[[15]] 等来访问元素。然后你可以根据你想要的处理列表。

    【讨论】:

      【解决方案3】:

      这是使用lapply() 的策略。将您的代码转换为匿名函数,将 AllVotes[[1]] 替换为函数的参数,例如 list。

      AllVotes <- readRDS('FinalVotes.rds')
      out_list <- lapply(AllVotes, function(list) {
        c(unlist(sapply(names(list$votes$'+'$groups),
            function(x) unlist(list$votes$'+'$groups[[x]]))),        
            unlist(sapply(names(list$votes$'-'$groups), 
            function(x) unlist(list$votes$'-'$groups[[x]]))),
            unlist(sapply(names(list$votes$'0'$groups),
            function(x) unlist( list$votes$'0'$groups[[x]]))))
         })
      

      我使用 rbenchmark 包将我的答案与 Duck 的 for 循环答案进行了基准测试,以运行每个版本 100 次。 benchmark() 的两个参数是我定义为这两种方法的包装器的函数,因此输出会更容易解释。

      library(rbenchmark)
      benchmark(lapply_test(), loop_test())
                 test replications elapsed relative user.self sys.self
      1 lapply_test()          100  102.08    1.043    100.91     0.25  
      2   loop_test()          100   97.86    1.000     96.87     0.15   
      

      看起来这两种方法在我的机器上几乎是等效的,每次迭代大约需要 1 秒。 loop 版本稍微快一点,但差异非常小,不同的机器可能会更快地运行 lapply 版本。对于非常大的文件,其中之一可能会成为明显的赢家。此外,由于文件非常大,lapply 版本很容易并行化。

      【讨论】:

        猜你喜欢
        • 2020-09-17
        • 2019-03-18
        • 2016-05-19
        • 2020-08-11
        • 2019-10-22
        • 1970-01-01
        • 2016-02-02
        • 1970-01-01
        • 2015-12-25
        相关资源
        最近更新 更多