2

I would like to group by and take the sum of the observations by time intervals of 10 minutes in R.

I have looked into cut, and to.period from the xts package and requires OHLC data (Open High Low Close stock data) - I have a different time series data set.

Python code:

data_timeidx = data.set_index('timestamp')
data_time_grp = data_timeidx.groupby(pd.Grouper(freq='10Min'))

Data:

start <- as.POSIXct("2015-12-05 06:00:00", "%Y-%m-%d %H:%M")
end   <- as.POSIXct("2015-12-05 08:00:00", "%Y-%m-%d %H:%M")

time <- seq(start, end, by = 0.05)
d <- data.frame(time)
obs <- nrow(d)
d$data <- rnorm(obs)
user8959427
  • 2,027
  • 9
  • 20

1 Answers1

4

This is a straight forward problem using the cut function and the dplyr package:

library(dplyr)
d %>% group_by(group10min=cut(time, "10 min")) %>% summarize(sum=sum(data))



# A tibble: 13 x 2
   group10min              sum
   <fct>                 <dbl>
 1 2015-12-05 06:00:00   63.3 
 2 2015-12-05 06:10:00    9.68
 3 2015-12-05 06:20:00 -139.  
 4 2015-12-05 06:30:00  -18.4 
 5 2015-12-05 06:40:00 -104.  
 6 2015-12-05 06:50:00   28.7 
 7 2015-12-05 07:00:00 -146.  
Dave2e
  • 22,192
  • 18
  • 42
  • 50