我正在尝试使用 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
有没有使用 tidyverse 和 dplyr 中的 group_by_at 或 group_by_all 函数等类似函数来缩短这个步骤的方法?非常感谢您的帮助。
library(tables); tabular(sex + 1 ~ age * mean, dsn)
。 - G. Grothendieck