【问题标题】:Aggregating frequency tables in R在 R 中聚合频率表
【发布时间】:2021-02-24 16:10:51
【问题描述】:

我想将数据框A、B、C按行列聚合得到D。

A <- data.frame(A = c("John","Fred","Paul"), Money = c(5,20,10), Hats = c(1,2,2))
B <- data.frame(A = c("John","Fred"), Money = c(15,10), Hats = c(1,2))
C <- data.frame(A = c("Paul"), Money = c(20), Hats = c(1))

D <- data.frame(A = c("John","Fred","Paul"), Money = c(20,30,30), Hats = c(2,3,3))

R 中哪一种是最快的方式?

【问题讨论】:

    标签: r dataframe join dplyr aggregate


    【解决方案1】:

    你可以这样做:

    aggregate(.~A, do.call(rbind,list(A,B,C)), sum)
    
         A Money Hats
    1 Fred    30    4
    2 John    20    2
    3 Paul    30    3
    

    或者干脆

    aggregate(.~A, rbind(A,B,C), sum)
    
         A Money Hats
    1 Fred    30    4
    2 John    20    2
    3 Paul    30    3
    

    【讨论】:

      【解决方案2】:

      使用 dplyr:

      library(dplyr)
      bind_rows(A,B,C) %>% group_by(A) %>% summarise(Money = sum(Money), Hats = sum(Hats))
      `summarise()` ungrouping output (override with `.groups` argument)
      # A tibble: 3 x 3
        A     Money  Hats
        <chr> <dbl> <dbl>
      1 Fred     30     4
      2 John     20     2
      3 Paul     30     3
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-09
        • 1970-01-01
        相关资源
        最近更新 更多