【问题标题】:Having trouble to use the plyr package and working with lists无法使用 plyr 包和使用列表
【发布时间】:2013-01-21 13:21:30
【问题描述】:

我无法理解 plyr 包的用法。我尝试使用它来拆分存储在列表中的数据帧,应用函数,将结果存储为数据帧并将数据帧再次组合为列表。 因此,鉴于以下数据:

    #create test dfs
    df1<-data.frame(a=sample(1:50,10),b=sample(1:50,10),c=sample(1:50,10),d=(c("a","b","c","a","a","b","b","a","c","d")))
    df2<-data.frame(a=sample(1:50,9),b=sample(1:50,9),c=sample(1:50,9),d=(c("e","f","g","e","e","f","f","e","g")))
    df3<-data.frame(a=sample(1:50,8),b=sample(1:50,8),c=sample(1:50,8),d=(c("h","i","j","h","h","i","i","h")))

    #make them a list
    list.1<-list(df1=df1,df2=df2,df3=df3)

我想计算每个数据帧的 d 中定义的每个组的平均值。 如果我只在一个数据帧上使用 plyr(按组计算特定列的平均值),则使用 plyr 包的可能性是:

    ddply(df1,.(d),summarise, mean=mean(a))

但是如何将它应用于数据框中的每一列以及列表中的每个数据框?以及如何重新组合所有数据,以便最终获得包含结果的矩阵列表?对于这个非常基本的问题,我很抱歉,但我是 R 新手,我已经尝试解决这个问题已经有一段时间了......谢谢。

【问题讨论】:

    标签: r plyr


    【解决方案1】:

    您需要将所有数据放入一个大data.frame:

    library(reshape)
    
    big_dataframe = ldply(list.1, function(x) melt(x, id.vars = "d"))
    > head(big_dataframe)
      .id d variable value
    1 df1 a        a    44                                                      
    2 df1 b        a    17                                                      
    3 df1 c        a    15                                                      
    4 df1 a        a    30                                                      
    5 df1 a        a    49                                                      
    6 df1 b        a    33
    

    ...然后在上面使用ddply。

    res = ddply(big_dataframe, .(.id, d, variable), summarise, mn = mean(value))
    > res
       .id d variable       mn
    1  df1 a        a 40.00000                                                  
    2  df1 a        b 25.25000                                                  
    3  df1 a        c 31.25000                                                  
    4  df1 b        a 22.66667                                                  
    5  df1 b        b 16.00000                                                  
    6  df1 b        c 26.00000                                                  
    7  df1 c        a  9.00000                                                  
    8  df1 c        b 16.50000                                                  
    9  df1 c        c 15.00000                                                  
    10 df1 d        a 28.00000                                                  
    11 df1 d        b 24.00000                                                  
    12 df1 d        c 39.00000                                                  
    13 df2 e        a 18.50000                                                  
    14 df2 e        b 15.50000                                                  
    15 df2 e        c 16.50000                                                  
    16 df2 f        a 26.33333                                                  
    17 df2 f        b 42.00000                                                  
    18 df2 f        c 37.00000                                                  
    19 df2 g        a 26.50000                                                  
    20 df2 g        b 22.00000                                                  
    21 df2 g        c 31.00000                                                  
    22 df3 h        a 29.25000                                                  
    23 df3 h        b 34.25000                                                  
    24 df3 h        c 32.00000                                                  
    25 df3 i        a 30.33333                                                  
    26 df3 i        b 40.00000                                                  
    27 df3 i        c 24.33333                                                  
    28 df3 j        a 21.00000                                                  
    29 df3 j        b  5.00000                                                  
    30 df3 j        c 46.00000 
    

    它给出了每个变量 (a-c)、每个因子级别 d 和每个子数据帧 (df1-df3) 的平均值。

    【讨论】:

      【解决方案2】:

      你总是可以只lapply你的ddply:

       lapply(list.1, function(x)   ddply(x, .(d), function(x)  
                                   data.frame(a=mean(x$a),b=mean(x$b),c= mean(x$c))) )
      

      或完全使用您的代码:

      lapply(list.1, function(x) ddply(x,.(d),summarise, mean=mean(a)) )
      

      【讨论】:

      • 谢谢。这就是我在想的,但我不知道如何在 ddply 中命名该对象。现在有了内部函数,这一切都说得通了……我认为第一个代码最后错过了一个 ) 。它工作正常,但我必须手动索引所有列。 downer 代码只返回 a 的方法。
      【解决方案3】:

      这是一个结合llply() 和ddply() 的解决方案。首先,llply() 将函数应用于列表的每个元素并返回一个列表。然后将ddply()应用于列表的每个数据帧,并根据d列划分每个数据帧。函数colMeans()用于计算每个数值列的平均值。

      llply(list.1,function(x) ddply(x,.(d),function(x) colMeans(x[,1:3])))
      $df1
        d        a     b        c
      1 a 22.25000 26.25 34.25000
      2 b 19.66667 22.00 28.66667
      3 c 37.00000 44.50 18.00000
      4 d 17.00000  3.00  4.00000
      
      $df2
        d        a        b    c
      1 e 20.50000 32.25000 18.5
      2 f 25.33333 34.33333 21.0
      3 g 20.50000 26.50000 16.5
      
      $df3
        d    a        b        c
      1 h 17.5 26.50000 37.25000
      2 i 45.0 22.33333 26.33333
      3 j 25.0 33.00000 42.00000
      

      【讨论】:

      • 完美且易于使用。这就是我正在考虑的,但我不知道如何在 ddply 中命名该对象。像这样它现在可以工作了!谢谢。
      猜你喜欢
      • 1970-01-01
      • 2010-11-26
      • 2011-12-07
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      相关资源
      最近更新 更多