I have a data.frame
that looks like this.
x a 1
x b 2
x c 3
y a 3
y b 3
y c 2
I want this in matrix form so I can feed it to heatmap to make a plot. The result should look something like:
a b c
x 1 2 3
y 3 3 2
I have tried cast
from the reshape package and I have tried writing a manual function to do this but I do not seem to be able to get it right.
The question is some years old but maybe some people are still interested in alternative answers.
If you don't want to load any packages, you might use this function:
How it works:
There are many ways to do this. This answer starts with my favorite ways, but also collects various ways from answers to similar questions scattered around this site.
Using the tidyverse:
The cool new way to do this is with
spread
from tidyr. It returns a data frame, which is probably what most readers of this answer will want. For a heatmap, though, you would need to convert this to a true matrix.Using reshape2:
One of the first steps toward the tidyverse was the reshape2 package. I still think for many reshaping tasks, the
melt
and*cast
functions are cleaner and simpler than the tidyverse ways.To get a matrix use
acast
:Or to get a data frame, use
dcast
, as here: Reshape data for values in one column.Using plyr:
In between reshape2 and the tidyverse came
plyr
, with thedaply
function, as shown here: https://stackoverflow.com/a/7020101/210673Using matrix indexing:
This is kinda old school but is a nice demonstration of matrix indexing, which can be really useful in certain situations.
Using
xtabs
:Using a sparse matrix:
There's also
sparseMatrix
within theMatrix
package, as seen here: R - convert BIG table into matrix by column namesUsing
reshape
:You can also use the base R function
reshape
, as suggested here: Convert table into matrix by column names, though you have to do a little manipulation afterwards to remove an extra columns and get the names right (not shown).The tidyr package from the tidyverse has an excellent function that does this.
Assuming your variables are named v1, v2 and v3, left to right, and you data frame is named dat:
Ta da!
base R,
unstack
This may not be a general solution but works well in this case.
data