0

I have a matrix that looks like the "main" data frame below:

date<-c("2014-01-01","2014-02-01","2014-01-01","2014-03-01")
value<-c(1,2,3,4)
group<-c("a","a","b","b")
main<-data.frame(date= date,value = value, group = group)
main

main looks like this. You can run the code and see.

       date     value group
1 2014-01-01     1     a
2 2014-02-01     2     a
3 2014-01-01     3     b
4 2014-03-01     4     b

Is there a way to make get all the groups from the group column and make them individual columns so my final data frame would look like this with 3 columns.

      date       a    b
1 2014-01-01     1    3
2 2014-02-01     2   
4 2014-03-01          4

I think what I am looking for is the OPPOSITE of the melt function. But I don't see an UNMELT function.

Thank you.

user3022875
  • 8,598
  • 26
  • 103
  • 167

1 Answers1

0

Yup! This is a classic case for the dcast function from the reshape2 package.

The code would be:

dcast(main, date~group)
stanekam
  • 3,906
  • 2
  • 22
  • 34