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.
There are many ways to do this. This answer starts with what is quickly becoming the standard method, but also includes older methods and various other methods from answers to similar questions scattered around this site.
Using the tidyverse:
The new cool new way to do this is with
pivot_widerfromtidyr 1.0.0. 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.The old cool new way to do this is with
spreadfromtidyr. It similarly returns a data frame.Using reshape2:
One of the first steps toward the tidyverse was the reshape2 package.
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 thedaplyfunction, 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
sparseMatrixwithin theMatrixpackage, 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).