Simpler way to reconstitute a melted data frame back to the original

假如想象 提交于 2019-11-29 13:57:11
Ricardo Saporta

If you add some form of marker to indicate which original row an item belongs to, then it is easy:

require(reshape2)
iris$rn <- seq_len(nrow(iris))
molten  <- melt(iris, id.vars = c("Species", "rn"))

# just a one-liner
dcast(molten, rn + Species ~ variable)

The difficulty you are encountering is that there is no way to identify which items go together. Are the 1:5 rows in the molten set one row? or is it the 2:6 and the 1 is misplaced? Melted data is in fact, melted :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!