总结并计算DPLYR分组DF中唯一值的数量

发布于 2025-02-11 06:15:19 字数 823 浏览 3 评论 0原文

我有这个DF:

structure(list(CN = c("BR", "BR", "BR", "PL", "PL", "PL", 
"BR", "BR", "BR", "BR", "PL", "PL", "PL"), Year = c(2019, 
2019, 2019, 2019, 2019, 2019, 2020, 2020, 2020, 2020, 2020, 2020, 
2020), Squad = c("A", "B", "C", "A", "B", "C", "C", "F", "G", 
"I", "D", "E", "F"), X = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 
1), Y = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1)), row.names = c(NA, 
-13L), class = c("tbl_df", "tbl", "data.frame"))

我想总结CN和年分组的(X+Y和小队计数的总和);在相同的结构中,添加了一个仅由CN分组的小队的唯一/不同值计数的列。

看起来像这样:

structure(list(CN = c("BR", "BR", "PL", "PL"), Year = c(2019, 
2020, 2019, 2020), Sum = c(12, 14, 12, 12), n_squad = c(3, 4, 
3, 3), n_squad_distinct = c(6, 6, 6, 6)), row.names = c(NA, -4L
), class = c("tbl_df", "tbl", "data.frame"))

谢谢

I have this df:

structure(list(CN = c("BR", "BR", "BR", "PL", "PL", "PL", 
"BR", "BR", "BR", "BR", "PL", "PL", "PL"), Year = c(2019, 
2019, 2019, 2019, 2019, 2019, 2020, 2020, 2020, 2020, 2020, 2020, 
2020), Squad = c("A", "B", "C", "A", "B", "C", "C", "F", "G", 
"I", "D", "E", "F"), X = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 
1), Y = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1)), row.names = c(NA, 
-13L), class = c("tbl_df", "tbl", "data.frame"))

I want to summarize (sum of x+y and squad count) grouped by CN and Year; and in the same structure add a column with the count of unique/distinct values for squad grouped by CN only.

It would look like this:

structure(list(CN = c("BR", "BR", "PL", "PL"), Year = c(2019, 
2020, 2019, 2020), Sum = c(12, 14, 12, 12), n_squad = c(3, 4, 
3, 3), n_squad_distinct = c(6, 6, 6, 6)), row.names = c(NA, -4L
), class = c("tbl_df", "tbl", "data.frame"))

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆凄美了谁 2025-02-18 06:15:19

我们可以通过在'squead'上应用n_distinct来创建由'cn'分组的'n_squad_distinct'列,然后添加'Year''和'N_squad_distinct',也将“ n_squad_distinct”分组为分组变量

library(dplyr)
df %>%
   group_by(CN) %>%
   mutate(n_squad_distinct = n_distinct(Squad)) %>% 
   group_by(n_squad_distinct, Year, .add = TRUE) %>%
   summarise(Sum = sum(X + Y), n_squad = n_distinct(Squad), .groups = 'drop')

- 输出

# A tibble: 4 × 5
  CN    n_squad_distinct  Year   Sum n_squad
  <chr>            <int> <dbl> <dbl>   <int>
1 BR                   6  2019    12       3
2 BR                   6  2020    14       4
3 PL                   6  2019    12       3
4 PL                   6  2020    12       3

We may create the 'n_squad_distinct' column grouped by 'CN" by applying n_distinct on the 'Squad', then add the 'Year' and 'n_squad_distinct' also as grouping variables and do the summarise

library(dplyr)
df %>%
   group_by(CN) %>%
   mutate(n_squad_distinct = n_distinct(Squad)) %>% 
   group_by(n_squad_distinct, Year, .add = TRUE) %>%
   summarise(Sum = sum(X + Y), n_squad = n_distinct(Squad), .groups = 'drop')

-output

# A tibble: 4 × 5
  CN    n_squad_distinct  Year   Sum n_squad
  <chr>            <int> <dbl> <dbl>   <int>
1 BR                   6  2019    12       3
2 BR                   6  2020    14       4
3 PL                   6  2019    12       3
4 PL                   6  2020    12       3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文