Search code examples
rmeanaggregation

How to aggregate data one after another in r?


Let's say I have hourly (6 hours in total) wave height data like below. I am aggregating them into 2-hour and 3-hour heights using the mean function

h_1<-c(2, 4, 6, 7, 9, 11)

Here is the function I used:

 agg_direct<-function(x){
      
      y<-as.data.frame(x)
      names(y)<-c("wh")
      agg<-lapply(c(1, 2, 3),  function(x) 
        tapply(y$wh, as.integer(gl(nrow(y), x, nrow(y)) ), FUN = mean))
    }
out<-agg_direct(h_1)

Outputs of this:

    $h_1
 1  2  3  4  5  6 
 2  4  6  7  9 11 

$h_2
   1    2    3 
 3.0  6.5 10.0 

$h_3
1 2 
4 9

But here is what I exactly want:

For 2-h, mean of (2,4), (4,6), (6,7), (7,9), (9,11)
For 3-h, mean of (2,4,6), (4,6,7), (6,7,9), (7,9,11)

so the outputs will be like:

For 2-h: 3, 5, 6.5, 8, 10
For 3-h: 4, 5.66, 7.33, 9

And this process will be performed following 4-hour, 5-hour...


Solution

  • We can use rollmean from zoo

    library(zoo)
    rollmean(h_1, 2)
    #[1]  3.0  5.0  6.5  8.0 10.0
    rollmean(h_1, 3)
    #[1] 4.000000 5.666667 7.333333 9.000000