我有一个看起来像这样的数据库:
start<-as.POSIXct("2012-01-15") interval<-60 end<-start+as.difftime(31,units="days") date<-seq(from=start,by=interval*60, to=end) # date/time information l<-length(date) stations<-as.factor(rep(1:3,len=l)) # stations df<-data.frame(date,stations) # data frame
我想要的是将此数据帧中的站列重塑为几列(在本例中为 3 列),并计算每个站在每个日期/时间行中记录的时间数。但是,我想保留数据库中的原始日期/时间列。如果一个电台没有记录在一个特定的日期/时间,那么我想分配一个零值。
理想情况下,我想要这样的输出:
date 1 2 3 2012-01-15 0:00 1 0 0 2012-01-15 1:00 0 1 0 2012-01-15 2:00 0 0 1 2012-01-15 3:00 1 0 0 2012-01-15 4:00 0 1 0 2012-01-15 5:00 0 0 1 2012-01-15 6:00 1 0 0 2012-01-15 7:00 0 1 0 2012-01-15 8:00 0 0 1 2012-01-15 9:00 1 0 0 2012-01-15 10:00 0 1 0