代码之家  ›  专栏  ›  技术社区  ›  TimTeaFan

在R中总结组意味时如何有条件地创建新组

  •  2
  • TimTeaFan  · 技术社区  · 7 年前

    我有一些数据,我想对这些数据进行总结。然后我想重新分组一些较小的组(匹配某个n<x条件)分为一组,称为“其他”。我找到了一个办法。但感觉外面有更有效的解决方案。我想知道data.table方法如何解决这个问题。

    # preps
    library(tibble)
    library(dplyr)
    set.seed(7)
    
    # generate 4 groups with more observations
    tbl_1  <- tibble(group = rep(sample(letters[1:4], 150, TRUE), each = 4),
                     score = sample(0:10, size = 600, replace = TRUE))
    
    # generate 3 groups with less observations
    tbl_2 <- tibble(group = rep(sample(letters[5:7], 50, TRUE), each = 3),
                    score = sample(0:10, size = 150, replace = TRUE)) 
    
    # put them into one data frame
    tbl <- rbind(tbl_1, tbl_2)
    
    # aggregate the mean scores and count the observations for each group
    tbl_agg1 <- tbl %>%
      group_by(group) %>%
      summarize(MeanScore = mean(score),
                n = n())
    

    到目前为止很容易。

    # First, calculate summary stats for groups less then n < 100
    tbl_agg2 <- tbl_agg1 %>%
       filter(n<100) %>%
          summarize(MeanScore = weighted.mean(MeanScore, n),
                    sumN = sum(n))
    

    注:以上计算有误,现已更正(@Frank:谢谢你发现!)

    # Second, delete groups less then n < 100 from the aggregate table and add a row containing the summary statistics calculated above instead
    tbl_agg1 <- tbl_agg1 %>%
       filter(n>100) %>%
          add_row(group = "others", MeanScore = tbl_agg2[["MeanScore"]], n = tbl_agg2[["sumN"]])
    

    tbl\u agg1基本上显示了我希望它显示的内容,但我想知道是否有一种更平滑、更有效的方法来实现这一点。同时,我想知道data.table方法如何处理手头的问题。

    我欢迎任何建议。

    1 回复  |  直到 7 年前
        1
  •  6
  •   Frank    7 年前

    你对“其他”组的计算是错误的,我猜。。。应该是。。。

    tbl_agg1 %>% {bind_rows(
       filter(., n>100),
       filter(., n<100) %>%
       summarize(group = "other", MeanScore = weighted.mean(MeanScore, n), n = sum(n))
    )}
    

    但是,通过使用不同的分组变量,可以从一开始就让事情简单得多:

    tbl %>% 
      group_by(group) %>% 
      group_by(g = replace(group, n() < 100, "other")) %>% 
      summarise(n = n(), m = mean(score))
    
    # A tibble: 5 x 3
      g         n     m
      <chr> <int> <dbl>
    1 a       136  4.79
    2 b       188  4.49
    3 c       160  5.32
    4 d       116  4.78
    5 other   150  5.42
    

    或使用data.table

    library(data.table)
    DT = data.table(tbl)
    DT[, n := .N, by=group]
    DT[, .(.N, m = mean(score)), keyby=.(g = replace(group, n < 100, "other"))]
    
           g   N        m
    1:     a 136 4.786765
    2:     b 188 4.489362
    3:     c 160 5.325000
    4:     d 116 4.784483
    5: other 150 5.420000