Aggregate time column on hourly interval in R

元气小坏坏 提交于 2019-12-22 10:18:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!