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

Tim*_*Fan 2 r dplyr data.table tibble

我有数据,我想总结一下组的意思.然后,我想将一些较小的组(匹配某个n <x条件)重新组合成一个名为"其他"的组.我找到了一种方法来做到这一点.但感觉有更有效的解决方案.我想知道data.table方法如何解决问题.

以下是使用tibble和dyplr的示例.

# 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())
Run Code Online (Sandbox Code Playgroud)

到目前为止这么容易.接下来我想只显示超过100个观察组.所有其他组应合并为一个名为"其他"的组.

# 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))
Run Code Online (Sandbox Code Playgroud)

注意:上面的计算中有一个错误,现在已经纠正了(@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"]])
Run Code Online (Sandbox Code Playgroud)

tbl_agg1基本上显示了我想要它显示的内容,但我想知道是否有更平滑,更有效的方法来执行此操作.与此同时,我想知道data.table方法如何处理手头的问题.

我欢迎任何建议.

Fra*_*ank 6

您对"其他"组的计算是错误的,我猜...应该......

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

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

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
Run Code Online (Sandbox Code Playgroud)

或者使用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
Run Code Online (Sandbox Code Playgroud)