I want to reshape a very simple dataframe and can't find the way to do that
I tried this solution: Transposing data frames and all related solutions, with no luck
I have a tidy dataframe who looks like this:
df<-rbind(c('a','x1'),c('a','x2'),c('a','x3'),
c('b','x6'),c('b','x7'))
colnames(df)<-c('var','val')
And tried
library(dplyr)
library(tidyr)
df %>%
gather(var, val, 2:ncol(df)) %>%
spread_(names(data)[1], "val")
I want a dataframe that looks like:
a x1 x2 x3
b x6 x7 NA
But I get this error
Error: Each row of output must be identified by a unique combination of keys