简体   繁体   English

Group_by(dplyr),其中一个因素作为列

[英]Group_by (dplyr) with one factor as column

In the following dummy data set, I would like to create a summary table grouping over three variables. 在下面的虚拟数据集中,我想创建一个汇总表,将三个变量分组。 So far, I used dplyr . 到目前为止,我使用dplyr However, I would like to have the values of r as the column names, and the x1 and x2 valuesas the row names, with the respective cells filled by values from m. 但是,我想将r的值作为列名,将x1和x2的值作为行名,并用m中的值填充各个单元格。 What could be a solution? 有什么解决方案?

r <- rep(seq(1,10,1),10)
x1 <- rbinom(100, 1, 0.5)
x2 <- rbinom(100, 2, 0.5)
y <- rnorm(100, 10, 5)

df <- data.frame(r,x1,x2,y)

library(dplyr)

View(df %>% 
  group_by(x1,x2,r) %>% 
  summarise(m = mean(y))
)

In order to achieve your desired output you basically have to transform from long to wide fromat, using x1 and x2 as grouping variables. 为了获得所需的输出,您基本上必须使用x1和x2作为分组变量,从long转换为wide。 Function spread() will do the job: 函数spread()将完成以下工作:

library(dplyr)
library(tidyr)

df %>% 
  group_by(x1,x2,r) %>% 
  summarise(m = mean(y)) %>% 
  ungroup %>% 
  spread(key = r, value = m,-x1, -x2)

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

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