Search code examples
rtime-seriesmeanbinning

Timeseries average based on a defined time interval (bin)


Here is an example of my dataset. I want to calculate bin average based on time (i.e., ts) every 10 seconds. Could you please provide some hints so that I can carry on?

In my case, I want to average time (ts) and Var in every 10 seconds. For example, I will get an averaged value of Var and ts from 0 to 10 seconds; I will get another averaged value of Var and ts from 11 to 20 seconds, etc.

 df = data.frame(ts = seq(1,100,by=0.5), Var = runif(199,1, 10))

Any functions or libraries in R can I use for this task?


Solution

  • There are many ways to calculate a binned average: with base aggregate,by, with the packages dplyr, data.table, probably with zoo and surely other timeseries packages...

    library(dplyr)
    df %>%
        group_by(interval = round(df$ts/10)*10) %>%
        summarize(Var_mean = mean(Var))
    # A tibble: 11 x 2
       interval Var_mean
          <dbl>    <dbl>
     1        0 4.561653
     2       10 6.544980
     3       20 6.110336
     4       30 4.288523
     5       40 5.339249
     6       50 6.811147
     7       60 6.180795
     8       70 4.920476
     9       80 5.486937
    10       90 5.284871
    11      100 5.917074
    

    That's the dplyr approach, see how it and data.table let you name the intermediate variables, which keeps code clean and legible.