问题
I have a df in R in the following format. What could be the easiest way to aggregate this on an hourly time interval basis, currently it is every minute
theTime24 Amount
988 2015-02-04 23:53:00 2
989 2015-02-04 23:55:00 1
990 2015-02-04 23:56:00 3
991 2015-02-04 23:57:00 2
992 2015-02-04 23:58:00 1
993 2015-02-04 23:59:00 2
回答1:
(Since the user (@user20650) who answered added it as a comment, I am answering my own question)
aggregate(Amount ~ format(as.POSIXct(countByCountryTime$theTime24), "%Y-%m-%d %H"), data=countByCountryTime, sum)
回答2:
I would format the date as POSIX, convert to hourly, and aggregate.
dt$theTIME24<-as.POSIXct(strptime(dt$theTIME24,format="%Y-%m-%d %H:%M:%S"))
dt$theTIME24.h<-as.POSIXct(strptime(dt$theTIME24,format="%Y-%m-%d %H"))
amt<-aggregate(dt$Amount,by=list(dt$theTIME24.h),mean)
来源:https://stackoverflow.com/questions/28334342/aggregate-time-column-on-hourly-interval-in-r