[英]Summarise multiple variables by one group at a time
There are a number of questions and answers about summarising multiple variables by one or more groups (eg, Means multiple columns by multiple groups ). 关于通过一个或多个组汇总多个变量有许多问题和答案(例如, 多个组表示多个列 )。 I don't think this is a duplicate. 我不认为这是重复的。
Here's what I'm trying to do: I want to calculate the mean for 4 variables by Displacement
, then calculate the mean for those same three by Horsepower
, and so on. 这是我正在尝试做的事情:我想通过Displacement
计算4个变量的平均值,然后通过Horsepower
计算这三个变量的平均值,依此类推。 I don't want to group by vs
, am
, gear
, and carb
simultaneously (ie, I'm not looking for simply mydata %>% group_by(vs, am, gear, and carb) %>% summarise_if(...)
. 我不想 GROUP BY vs
, am
, gear
和carb
同时(即,我不是在寻找简单mydata %>% group_by(vs, am, gear, and carb) %>% summarise_if(...)
。
How can I calculate the means for a set of variables by Displacement
, then calculate the means for that same set of variables by Horsepower
, etc., then place in a table side by side? 我如何计算装置,用于通过一组变量Displacement
, 然后计算出用于由相同的变量Horsepower
,等等, 然后放在并排表的一面呢?
I tried to come up with a reproducible example but couldn't. 我试图提出一个可重复的例子,但不能。 Here is a tibble from mtcars
that shows what I'm ultimately looking for (data is made up): 这是来自mtcars
一个mtcars
,它显示了我最终要寻找的东西(数据组成):
tibble(Item = c("vs", "am" ,"gear", "carb"),
"Displacement (mean)" = c(2.4, 1.4, 5.5, 1.3),
"Horsepower (mean)" = c(155, 175, 300, 200))
Perhaps something like this using purrr::map
and some rlang
syntax? 也许这样使用purrr::map
和一些rlang
语法?
grps <- list("cyl", "vs")
map(setNames(grps, unlist(grps)), function(x)
mtcars %>%
group_by(!!rlang::sym(x)) %>%
summarise(mean.mpg = mean(mpg), mean.disp = mean(disp)) %>%
rename(id.val = 1)) %>%
bind_rows(.id = "id")
## A tibble: 5 x 4
# id id.val mean.mpg mean.disp
# <chr> <dbl> <dbl> <dbl>
#1 cyl 4. 26.7 105.
#2 cyl 6. 19.7 183.
#3 cyl 8. 15.1 353.
#4 vs 0. 16.6 307.
#5 vs 1. 24.6 132.
With so few groupings, why not do each set of means one at a time: 如此少的分组,为什么不一次做一套方法:
out1 <- mydata %>% group_by(Var1) %>%
summarise(mean_1a = mean(var_a), mean_1b = mean(var_b))
out2 <- mydata %>% group_by(Var2) %>%
summarise(mean_2a = mean(var_a), mean_2b = mean(var_b))
out3 <- mydata %>% group_by(Var3) %>%
summarise(mean_3a = mean(var_a), mean_3b = mean(var_b))
If it makes sense to place the results side-by-side, you could do so with something like: 如果将结果并排放置是有意义的,你可以这样做:
result <- cbind(out1, out2, out3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.