These answers are great if you have one variable to summarize by. What about two? I want to summarize across one but leave the other as is. The above solutions do not work in this case because the data frame still needs to be grouped.
#Data set up
set.seed(3243242)
dsn <- tibble(
obese = sample(c(TRUE, FALSE), size=100, replace = TRUE),
sex = sample(c("M", "F"), size=100, replace=TRUE),
age = rnorm(n=100, mean=20 + 4*(sex=="F"), sd=0.1)
)
library("tidyverse")
I restated the original problem using 2 group_by variables.
#Extend to 2 group_by variables?
df1 <- dsn %>%
group_by(sex, obese) %>%
summarise(mean_age = mean(age)) %>%
ungroup()
#Also across sex
df2 <- dsn %>%
group_by(obese) %>%
summarise(mean_age = mean(age)) %>%
ungroup()
#Final_result:
bind_rows(df1, df2)
Way to do this in one step? You can add mean
with add_row()
but not with a grouped df. Another option is to create a function that does all the things on the group dataset. If there are other things you want to do, say sort or create new variables, you can do it in the function. Then, you can apply the function to each grouped dataset. After combining via dplyr::bind_rows()
, you can change the missing group variable to all via tidyr::replace_na()
.
#'@param df_group A grouped tibble
find_summary <- function(df_group){
df_group %>%
summarize(mean_age = mean(age)) #add other dplyr verbs here as needed like arrange or mutate
}
bind_rows(
find_summary(group_by(dsn, sex, obese)),
find_summary(group_by(dsn, obese))
) %>%
replace_na(list(sex = "all"))
sex obese mean_age
<chr> <lgl> <dbl>
1 F FALSE 24.0
2 F TRUE 24.0
3 M FALSE 20.0
4 M TRUE 20.0
5 all FALSE 21.7
6 all TRUE 22.3
You can extend the idea if you want a summary of all variables, by one variable, and by two variables.
bind_rows(
find_summary(group_by(dsn, sex, obese)),
find_summary(group_by(dsn, obese)),
find_summary(dsn)
) %>%
replace_na(list(sex = "all", obese = "all"))
sex obese mean_age
<chr> <chr> <dbl>
1 F FALSE 24.0
2 F TRUE 24.0
3 M FALSE 20.0
4 M TRUE 20.0
5 all FALSE 21.7
6 all TRUE 22.3
7 all all 22.0