Reverse melting operation with reshape2

Consider the following code.

library (reshape2) x = rnorm (20) y = x + rnorm (rnorm (20, sd = .01)) dfr <- data.frame (x, y) mlt <- melt (dfr) 

When I try to cancel this operation using dcast,

 dcast (mlt, value ~ variable) 

I get instead a data frame with three columns (for example, not suitable for layout). How to restore the original data frame using dcast?

+4
source share
1 answer

How to know R to know the order that existed before the melt? that is, the idea that the first row of x same as row 1 of y .

If you add an index column (since R will complain about duplicate .names rows), you can do this operation simply:

 dfr$idx <- seq_along(dfr$x) mlt <- melt(dfr, id.var='idx') dcast(mlt, idx ~ variable, value.var='value') 
+3
source

All Articles