dplyr总结了不扁平数据的组

发布于 2025-01-29 07:15:22 字数 1959 浏览 4 评论 0原文

我有一个数据集:

df <- structure(list(ID = c(101188, 101192, 101193, 101196, 101198, 
101202, 101203, 101206, 101211, 101212, 101216, 101219, 101220, 
101222, 101223, 101224, 101226, 101227, 101228, 101229), LA = c("Barking and Dagenham", 
"Barking and Dagenham", "Barking and Dagenham", "Barking and Dagenham", 
"Barking and Dagenham", "Barking and Dagenham", "Barking and Dagenham", 
"Barking and Dagenham", "Barking and Dagenham", "Barking and Dagenham", 
"Barking and Dagenham", "Barking and Dagenham", "Barking and Dagenham", 
"Barking and Dagenham", "Barking and Dagenham", "Barking and Dagenham", 
"Barking and Dagenham", "Barking and Dagenham", "Barking and Dagenham", 
"Barking and Dagenham"), EstablishmentGroup = c("Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools")), row.names = c(NA, -20L
), class = c("tbl_df", "tbl", "data.frame"))

如果运行以下代码,我希望最终的总结将数据弄平并告诉我

df %>%
  group_by(LA) %>%
  mutate(All_schools = n()) %>%
  ungroup() %>%
  group_by(LA, EstablishmentGroup, All_schools) %>%
  summarise(total = n(),
            per = total/All_schools)

Barking和Dagenham地方当局维护学校20 20 1

但它给了我20行。我可以使用独特的,但不确定我做错了什么。

I have a dataset:

df <- structure(list(ID = c(101188, 101192, 101193, 101196, 101198, 
101202, 101203, 101206, 101211, 101212, 101216, 101219, 101220, 
101222, 101223, 101224, 101226, 101227, 101228, 101229), LA = c("Barking and Dagenham", 
"Barking and Dagenham", "Barking and Dagenham", "Barking and Dagenham", 
"Barking and Dagenham", "Barking and Dagenham", "Barking and Dagenham", 
"Barking and Dagenham", "Barking and Dagenham", "Barking and Dagenham", 
"Barking and Dagenham", "Barking and Dagenham", "Barking and Dagenham", 
"Barking and Dagenham", "Barking and Dagenham", "Barking and Dagenham", 
"Barking and Dagenham", "Barking and Dagenham", "Barking and Dagenham", 
"Barking and Dagenham"), EstablishmentGroup = c("Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools", "Local authority maintained schools", 
"Local authority maintained schools")), row.names = c(NA, -20L
), class = c("tbl_df", "tbl", "data.frame"))

If I run the following code I expect the final summarise to flatten the data and tell me

df %>%
  group_by(LA) %>%
  mutate(All_schools = n()) %>%
  ungroup() %>%
  group_by(LA, EstablishmentGroup, All_schools) %>%
  summarise(total = n(),
            per = total/All_schools)

Barking and Dagenham Local authority maintained schools 20 20 1

But it gives me 20 rows instead. I could use a distinct, but not sure what I've done wrong.

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

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

发布评论

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

评论(1

乱了心跳 2025-02-05 07:15:22

您可以先总结计数,然后突变以计算百分比。

df %>%
  group_by(LA) %>%
  mutate(All_schools = n()) %>%
  ungroup() %>% 
  group_by(LA, EstablishmentGroup, All_schools) %>% 
  summarise(total = n()) %>% 
  mutate(per = total/All_schools)

输出:

# A tibble: 1 x 5
# Groups:   LA, EstablishmentGroup [1]
  LA                   EstablishmentGroup                 All_schools total   per
  <chr>                <chr>                                    <int> <int> <dbl>
1 Barking and Dagenham Local authority maintained schools          20    20     1

You can summarise the count first, then mutate to calculate the percentage.

df %>%
  group_by(LA) %>%
  mutate(All_schools = n()) %>%
  ungroup() %>% 
  group_by(LA, EstablishmentGroup, All_schools) %>% 
  summarise(total = n()) %>% 
  mutate(per = total/All_schools)

Output:

# A tibble: 1 x 5
# Groups:   LA, EstablishmentGroup [1]
  LA                   EstablishmentGroup                 All_schools total   per
  <chr>                <chr>                                    <int> <int> <dbl>
1 Barking and Dagenham Local authority maintained schools          20    20     1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文