Reshaping a column from a data frame into several

2019-03-04 11:46发布

问题:

This question already has an answer here:

  • Faster ways to calculate frequencies and cast from long to wide 4 answers

I have a database that looks like this:

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

What I would like is to reshape the station column from this data frame into several columns (in this example it will be 3 columns) and calculate the number of time each station was recorded in each date/time row. However, I would like to keep the original date/time column from the data base. If a station was not recorded in one specific date/time, then I want to assign a value of zero.

Ideally, I would like an output like this:

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

回答1:

You can try to use function dcast() from library reshape2.

library(reshape2)
dcast(df,date~stations,length)
                   date 1 2 3
1   2012-01-15 00:00:00 1 0 0
2   2012-01-15 01:00:00 0 1 0
3   2012-01-15 02:00:00 0 0 1
4   2012-01-15 03:00:00 1 0 0


回答2:

You could use the function xtabs:

xtabs( ~ date + stations, df)