How do I create data.framethat I melted with reshape2?
Reproducible example
library(reshape2)
library(plyr)
data(iris)
df <- melt(iris, id.vars="Species")
head(df)
Species variable value
1 setosa Sepal.Length 5.1
2 setosa Sepal.Length 4.9
3 setosa Sepal.Length 4.7
4 setosa Sepal.Length 4.6
5 setosa Sepal.Length 5.0
6 setosa Sepal.Length 5.4
What I tried with dcast
dcast(df, Species~variable, value.var = "value")
temporary solution
ddply(df, .(Species), function(x) {
Species <- unique(x$Species)
x$id <- 1:dim(x)[1]
x$Species <- NULL
dat <- unstack(x, value~variable)
dat$Species <- Species
return(dat)
})
What am I missing? This is something obvious, but I can not understand the answer. Perhaps I even answered that for someone else here. Argh.
source
share