【问题标题】:Mean all other other columns based on a single column in r表示基于 r 中单个列的所有其他列
【发布时间】:2015-05-26 07:02:03
【问题描述】:

我有一个包含 40,000 多列的大型数据框,我遇到了与此类似的问题 Sum by distinct column value in R

shop <- data.frame( 
  'shop_id' = c('Shop A', 'Shop A', 'Shop A', 'Shop B', 'Shop C', 'Shop C'), 
  'Assets' = c(2, 15, 7, 5, 8, 3),
  'Liabilities' = c(5, 3, 8, 9, 12, 8),
  'sale' = c(12, 5, 9, 15, 10, 18), 
  'profit' = c(3, 1, 3, 6, 5, 9))

我有一个重复多次的列 shop_id。我还有与该 shop_id 相关的其他值,例如资产、负债、利润、损失等。我现在想要对具有相同 shop_id 的所有变量进行平均,即,我想要唯一的 shop_ids 并希望对所有具有的列进行平均相同的 shop_id。因为,有数千个变量(列)分别处理每一列(变量)是非常乏味的。

我的答案应该是

 shop_id  Assets  Liabilities     sale    profit    
 Shop A   8.0     5.333333    8.666667  2.333333
 Shop B   5.0     9.000000   15.000000  6.000000
 Shop C   5.5    10.000000   14.000000  7.000000

我目前正在使用嵌套的 for 循环,如下所示: 像 R 一样多才多艺,我相信应该有更快的方法来做到这一点

idx <- split(1:nrow(shop), shop$shop_id)

newdata <- data.frame()

for( i in 1:length(idx)){
    newdata[i,1]<-c(names(idx)[i] )
    for (j in 2:ncol(shop)){
        newdata[i,j]<-mean(shop[unlist(idx[i]),j])
    }
}

【问题讨论】:

    标签: r dataframe unique average


    【解决方案1】:

    试试data.table

    library(data.table)
    setDT(shop)[, lapply(.SD, mean), shop_id]
    #  shop_id Assets Liabilities      sale   profit
    #1:  Shop A    8.0    5.333333  8.666667 2.333333
    #2:  Shop B    5.0    9.000000 15.000000 6.000000
    #3:  Shop C    5.5   10.000000 14.000000 7.000000
    

    或者

    library(dplyr)
    shop %>% 
        group_by(shop_id)%>%
        summarise_each(funs(mean))
    # shop_id Assets Liabilities      sale   profit
    #1  Shop A    8.0    5.333333  8.666667 2.333333
    #2  Shop B    5.0    9.000000 15.000000 6.000000
    #3  Shop C    5.5   10.000000 14.000000 7.000000
    

    或者

    aggregate(.~shop_id, shop, FUN=mean)
    #   shop_id Assets Liabilities      sale   profit
    #1  Shop A    8.0    5.333333  8.666667 2.333333
    #2  Shop B    5.0    9.000000 15.000000 6.000000
    #3  Shop C    5.5   10.000000 14.000000 7.000000
    

    对于 40,000 列,我会使用 data.table 或者可能是 dplyr。

    【讨论】:

      【解决方案2】:

      试试dplyr:

      library("dplyr")
      shop %>% group_by(shop_id) %>% summarise_each(funs(mean))
      
      #   shop_id Assets Liabilities      sale   profit
      # 1  Shop A    8.0    5.333333  8.666667 2.333333
      # 2  Shop B    5.0    9.000000 15.000000 6.000000
      # 3  Shop C    5.5   10.000000 14.000000 7.000000
      

      【讨论】:

        【解决方案3】:

        rowsum 可能会有所帮助,在这里:

        rowsum(shop[-1], shop[[1]]) / table(shop[[1]])
        #       Assets Liabilities      sale   profit
        #Shop A    8.0    5.333333  8.666667 2.333333
        #Shop B    5.0    9.000000 15.000000 6.000000
        #Shop C    5.5   10.000000 14.000000 7.000000
        

        【讨论】:

        • 这是一些创新的想法
        【解决方案4】:

        使用plyr 包中的ddply 函数:

        > require("plyr")
        > ddply(shop, ~shop_id, summarise, Assets=mean(Assets),
                Liabilities=mean(Liabilities), sale=mean(sale), profit=mean(profit))
        
          shop_id Assets Liabilities      sale   profit
        1  Shop A    8.0    5.333333  8.666667 2.333333
        2  Shop B    5.0    9.000000 15.000000 6.000000
        3  Shop C    5.5   10.000000 14.000000 7.000000
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-17
          相关资源
          最近更新 更多