Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting summary by group and overall using tidyverse

I am trying to find a way to get summary stats such as means by group and overall in one step using dplyr

#Data set-up
sex <- sample(c("M", "F"), size=100, replace=TRUE)
age <- rnorm(n=100, mean=20 + 4*(sex=="F"), sd=0.1)
dsn <- data.frame(sex, age)


library("tidyverse")

#Using dplyr to get means by group and overall
mean_by_sex <- dsn %>% 
  group_by(sex) %>% 
  summarise(mean_age = mean(age))

mean_all <- dsn %>% 
  summarise(mean_age = mean(age)) %>% 
  add_column(sex = "All")

#combining the results by groups and overall
final_result <- rbind(mean_by_sex, mean_all)
final_result  
#> # A tibble: 3 x 2
#>   sex   mean_age
#>   <fct>    <dbl>
#> 1 F         24.0
#> 2 M         20.0
#> 3 All       21.9
#This is the table I want but I wonder if is the only way to do this

Is there a way this in shorter step using group_by_at or group_by_all or a similar functions using tidyverse and dplyr Any help would be greatly appreciated

like image 916
SimRock Avatar asked Sep 03 '25 04:09

SimRock


2 Answers

One option could perhaps be:

dsn %>%
 group_by(sex) %>%
 summarise(mean_age = mean(age)) %>%
 add_row(sex = "ALL", mean_age = mean(dsn$age))

  sex   mean_age
  <fct>    <dbl>
1 F         24.0
2 M         20.0
3 ALL       21.9
like image 70
tmfmnk Avatar answered Sep 05 '25 00:09

tmfmnk


A little switching around can do it, too.

final_result <- dsn %>% 
  add_row(sex = "All", age = mean(age)) %>% 
  group_by(sex) %>% 
  summarise(mean_age = mean(age))
like image 42
vanao veneri Avatar answered Sep 05 '25 01:09

vanao veneri