如何重新创建data.frame我融化的reshape2?
可重复的例子
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
# Great, I'd like to get the original iris back
Run Code Online (Sandbox Code Playgroud)
我尝试过的 dcast
dcast(df, Species~variable, value.var = "value")
# should work but doesn't
Run Code Online (Sandbox Code Playgroud)
临时解决方案
# This works but clearly it shouldn't be this hard.
ddply(df, .(Species), function(x) {
Species <- …Run Code Online (Sandbox Code Playgroud)