This should be a simple exercise with reshape2
package in R but somehow I'm not seeing it.
Imagine I have data:
df <- data.frame(A = rnorm(4), B = rnorm(4))
which looks like:
A B
1 2.3729531 -0.9252266
2 0.9848229 -0.1152347
3 2.1234409 0.9035180
4 -0.5771637 1.2755104
long_form <- melt(df)
which looks like
variable value
1 A 2.3729531
2 A 0.9848229
3 A 2.1234409
4 A -0.5771637
5 B -0.9252266
6 B -0.1152347
7 B 0.9035180
8 B 1.2755104
How do I transform long_form
back into df
?
I can do this by adding another column first,
long_form = data.frame(id = c(1:4, 1:4), long_form)
dcast(long_form, id ~ variable)
and then drop the id column to recover df
; but it just seems like I should be able to do this without explicitly adding an id column to index the replicate A's and B's.