简体   繁体   中英

Aggregate by month,day of week hour and min in R

I have a folder with several files where the name of each file is the respective userID. Something like this:

         Time           Sms
 1 2012-01-01 00:00:00  10
 2 2012-01-01 00:30:00  11
 3 2012-01-01 01:00:00  13
 4 2012-01-01 01:30:00  10

How can i aggretate by moth, week, hour and minute? Something like this:

Month DayofWeek hour min  SMS
 1      Mon      0    0   14   <-mean 
 1      Mon      0   30   12
 1      Mon      1    0   17
 1      Mon      1   30   21
 .............................
 12    Sunday    23  30   12 

I had a similar issue aggregating hourly data into daily data. This is the code that worked for me.

fun <- function(s,i,j) { sum(s[i:(i+j-1)]) }

radday<-sapply(X=seq(1,24*nb_of_days,24),FUN=fun,s=your_time_series,j=24)

This sums data across a period j , which in my case since I was summing over 24 hours was 24. By changing the j value you can adjust it for your different periods of hour, day, week, month assuming that you have a constant period.

thanks for the help. I solved my problem by applying this code:

df<-aggregate(Sms~month(Time)+weekdays(Time)+hour(Time)+minute(Time),df,FUN='mean')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM