Hausladen Carina Hausladen Carina - 4 months ago 9
R Question

unbalanced dataframe from long to wide

I have a dataframe that looks like data_long:

data_long<-as.data.frame(matrix(nrow = 10, ncol = 2))
colnames(data_long)<-c("treatment","rolls")
data_long[,1]<-c(1,2,3,4,1,2,3,1,2,1)
data_long[,2]<-c(6,6,6,6,6,6,6,6,6,6)


I want to rearrange data_long to data_wide:

data_wide<-as.data.frame(matrix(nrow = 4, ncol=4))
colnames(data_wide)<-c("Treatment1","Treatment2","Treatment3","Treatment4")
data_wide[,1]<-c(6,6,6,6)
data_wide[,2]<-c(6,6,6,NA)
data_wide[,3]<-c(6,6,NA,NA)
data_wide[,4]<-c(6,NA,NA,NA)


I think the problem is, that I have unbalanced data. Any ideas? Thank you very much for your help!

Answer

You can use the spread function from the tidyr package:

library(dplyr)
library(tidyr)

data_long %>% 
  group_by(treatment) %>% 
  mutate(unique_id = 1:n()) %>% 
  spread(treatment, rolls)

In order for spread to work properly, each row within the treatment groups needs to have a unique identifier.