在 R 中汇总组均值时如何创建有条件的新组
How to create conditionally new groups when summarizing group means in R
我有一些数据,我想对其进行分组均值汇总。然后我想将一些较小的组(匹配某个 n < x 条件)重新分组到一个名为 "others" 的组中。我找到了一种方法来做到这一点。但感觉那里有更有效的解决方案。我想知道 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())
到目前为止很容易。
接下来,我只想显示具有超过 100 个观察值的组。所有其他组应合并为一个名为 "others" 的组。
# 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_agg1 基本上显示了我想要它显示的内容,但我想知道是否有更流畅、更有效的方法来做到这一点。同时我想知道 data.table 方法将如何处理手头的问题。
我欢迎任何建议。
你对"other"组的计算有误,我猜……应该是……
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
我有一些数据,我想对其进行分组均值汇总。然后我想将一些较小的组(匹配某个 n < x 条件)重新分组到一个名为 "others" 的组中。我找到了一种方法来做到这一点。但感觉那里有更有效的解决方案。我想知道 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())
到目前为止很容易。 接下来,我只想显示具有超过 100 个观察值的组。所有其他组应合并为一个名为 "others" 的组。
# 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_agg1 基本上显示了我想要它显示的内容,但我想知道是否有更流畅、更有效的方法来做到这一点。同时我想知道 data.table 方法将如何处理手头的问题。
我欢迎任何建议。
你对"other"组的计算有误,我猜……应该是……
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