如何为R中的每个日期的单个值列创建每个特定值的数据帧?

时间:2016-05-13 15:26:51

标签: r dataframe

我拥有的数据框格式为:

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

我想要这种格式的数据框:

> 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 

提前致谢!

2 个答案:

答案 0 :(得分:1)

Reshape2库具有此类应用程序的dcast功能

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

答案 1 :(得分:1)

你可以使用cast函数来完成它,这是一个更基础的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")

,并提供:

> 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

没关系,其他人发布了一个更好的dcast以上。