1

The dataframe I have is in the format:

dates <- c("02/27/92", "02/27/92", "02/28/92", "02/28/92", "02/28/92", "02/29/92", "02/29/92")

df_Before <- data.frame(Date = as.Date(dates, "%m/%d/%y"),
             ID = c(1,1,2,2,2,3,3),
             Var1 = factor(c('d','c','d','b','c','a','b')))
> df_Before
  Date     ID  Var1
1 1992-02-27  1    d
2 1992-02-27  1    c
3 1992-02-28  2    d
4 1992-02-28  2    b
5 1992-02-28  2    c
6 1992-02-29  3    a
7 1992-02-29  3    b

I would like a dataframe in this format:

> df_After    
  Date        ID  a  b  c  d
1 1992-02-27  1   0  0  1  1
2 1992-02-28  2   0  1  1  1
3 1992-02-29  3   1  1  0  0 

Thanks in advance!

Bret Shandro
  • 61
  • 1
  • 9

2 Answers2

1

Reshape2 library has the dcast function for this type of application

library(reshape2)
dcast(df_Before, Date+ID~Var1, length)

#        Date ID a b c d
#1 1992-02-27  1 0 0 1 1
#2 1992-02-28  2 0 1 1 1
#3 1992-02-29  3 1 1 0 0
Dave2e
  • 22,192
  • 18
  • 42
  • 50
1

You can do it with the cast function, which is a bit more base R:

library(reshape)

df_Before$values <- 1 # Need to add this one column in order to aggregate.
df_After <- cast(df_Before, formula = Date + ID ~ Var1, sum, value = "values")

Giving:

> df_After
        Date ID a b c d
1 1992-02-27  1 0 0 1 1
2 1992-02-28  2 0 1 1 1
3 1992-02-29  3 1 1 0 0

Never mind, someone else posted a better one with dcast up above.

giraffehere
  • 1,118
  • 7
  • 18