Assuming I have a melted data.frame
that looks like this:
variable value
1 A -0.19933093
2 A -1.19043346
3 A -1.32248172
4 A -1.98644507
5 A -0.07930953
6 B -0.10074686
7 B 0.72451483
8 B -0.40914044
9 B 0.02913376
10 B 0.16062491
How do I get it to:
A B
-0.19933093 -0.10074686
-1.19043346 0.72451483
-1.32248172 -0.40914044
-1.98644507 0.02913376
-0.07930953 0.16062491
Seems trivial but I am blanking on an answer. dcast
and acast
do not seem to do it. My goal is to do this on larger datasets and turn the end product into a matrix
with the column names to be the variable names. I tried playing with daply
and laply
(before melting) without much luck.
Using
acast()
to return a matrix. It needs an id variable.Alright, begin with a data frame in wide form, containing an id.
melt()
it to give the long form, thendcast()
it to get back to the original data frame.Try
unstack
:But I should add that I would love to know how to do this using
dcast
, as I've also tried repeatedly and haven't been able to.