简体   繁体   English

汇总多个 group_by 变量的组合和单独

[英]Summarise for multiple group_by variables combined and individually

I am using dplyr's group_by and summarise to get a mean by each group_by variable combined, but also want to get the mean by each group_by variable individually.我正在使用 dplyr 的 group_by 和汇总来获得每个 group_by 变量组合的平均值,但也想分别获得每个 group_by 变量的平均值。

For example if I run例如,如果我跑

mtcars %>% 
  group_by(cyl, vs) %>% 
  summarise(new = mean(wt))

I get我得到

    cyl    vs      new
  <dbl> <dbl>    <dbl>
     4     0 2.140000
     4     1 2.300300
     6     0 2.755000
     6     1 3.388750
     8     0 3.999214

But I want to get但我想得到

    cyl    vs      new
  <dbl> <dbl>    <dbl>
     4     0 2.140000
     4     1 2.300300
     4    NA 2.285727
     6     0 2.755000
     6     1 3.388750
     6    NA 3.117143
     8     0 3.999214
    NA     0 3.688556
    NA     1 2.611286

Ie get the mean for the variables both combined and individually即获得组合变量和单独变量的均值

Edit Jaap marked this as duplicate and pointed me in the direction of Using aggregate to apply several functions on several variables in one call .编辑Jaap 将此标记为重复,并指出使用聚合在一次调用中将多个函数应用于多个变量的方向。 I looked at jaap's answer there which referenced dplyr but I can't see how that answers my question?我在那里查看了引用 dplyr 的 jaap 的答案,但我看不出它是如何回答我的问题的? You say to use summarise_each , but I still don't see how I can use that to get the mean of each of my group by variables individually?您说要使用summarise_each ,但我仍然不明白如何使用它来分别通过变量获得每个组的平均值? Apologies if I am being stupid...抱歉,如果我是愚蠢的...

Here is an idea using bind_rows ,这是一个使用bind_rows的想法,

library(dplyr)

mtcars %>% 
     group_by(cyl, vs) %>% 
     summarise(new = mean(wt)) %>% 
    bind_rows(., 
              mtcars %>% group_by(cyl) %>% summarise(new = mean(wt)) %>% mutate(vs = NA), 
              mtcars %>% group_by(vs) %>% summarise(new = mean(wt)) %>% mutate(cyl = NA)) %>% 
   arrange(cyl) %>% 
   ungroup()

# A tibble: 10 × 3
#     cyl    vs      new
#   <dbl> <dbl>    <dbl>
#1      4     0 2.140000
#2      4     1 2.300300
#3      4    NA 2.285727
#4      6     0 2.755000
#5      6     1 3.388750
#6      6    NA 3.117143
#7      8     0 3.999214
#8      8    NA 3.999214
#9     NA     0 3.688556
#10    NA     1 2.611286

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM