0

I would like to reshape a data frame.

Data:

             a <- c("A_h:old","A_h:new","A_h:old","A_h:new","A_h:old","A_h:new","A_h:old","A_h:new")
             b <- c("2015-08-11","2015-08-11","2015-08-12","2015-08-12","2015-08-13","2015-08-13","2015-08-14","2015-08-14")
             c <- c(12,10,12,23,16,17,7,9)
             df <- data.frame(a,b,c)

Which produces:

              a         b          c
              A_h:old  2015-08-11  12
              B_h:new  2015-08-11  10
              A_h:old  2015-08-12  12
              B_h:new  2015-08-12  23
              A_h:old  2015-08-13  16
              B_h:new  2015-08-13  17
              A_h:old  2015-08-14   7
              B_h:new  2015-08-14   9

Desired outcome:

                  b        A_h:old   B_h:new
              2015-08-11   12         10
              2015-08-12   12         23
              2015-08-13   16         17
              2015-08-14    7         9

I tried to use:

             reshape(df, timevar = "b", idvar = c(" A_h:old", "B_h:new"), direction = "wide")

unsuccessfully.

1 Answers1

0

We can use dcast from reshape2 to convert from 'long' to 'wide'

library(reshape2)
dcast(df, b~a, value.var='c')
#           b A_h:new A_h:old
#1 2015-08-11      10      12
#2 2015-08-12      23      12
#3 2015-08-13      17      16
#4 2015-08-14       9       7
akrun
  • 874,273
  • 37
  • 540
  • 662