I have a data frame that looks like this:
class id
1 foo 1
2 bar 1
3 baz 1
4 baz 2
5 bar 2
6 foo 2
7 foo 3
8 foo 3
9 foo 3
My goal is to reshape it into a data frame that gathers the classes into a list, in the order that they are given. For example, the output would look like:
> output
id var1 var2 var3
1 1 foo bar baz
2 2 baz bar foo
3 3 foo foo foo
or, alternatively, a two-column data frame with the first column containing the id and the second column containing a list of the id
variables in order.
I've tried using dcast(test, id ~ class)
from the reshape library but that doesn't quite return the output that I need.
Any ideas of how to do this in R? Here is the data:
dput(test)
structure(list(class = c("foo", "bar", "baz", "baz", "bar", "foo",
"foo", "foo", "foo"), id = c(1, 1, 1, 2, 2, 2, 3, 3, 3)), row.names = c(NA,
-9L), class = "data.frame")