3
votes

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
2
You can use format to group by hour, along the lines of aggregate(Amount ~ format(as.POSIXct(dat$theTime24), "%Y-%m-%d %H"), data=dat, sum) [assuming the function is sum]user20650
@user20650 - Thanks you nailed it! Works as I expected :)K3rn3l5

2 Answers

3
votes

(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)
0
votes

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)