Hendrik
Hendrik

Reputation: 321

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. 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. 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))
)

Upvotes: 0

Views: 232

Answers (1)

Aleksandr
Aleksandr

Reputation: 1914

In order to achieve your desired output you basically have to transform from long to wide fromat, using x1 and x2 as grouping variables. Function spread() will do the job:

library(dplyr)
library(tidyr)

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

Upvotes: 1

Related Questions