How to calculate the mean for every n consecutive vectors and every for n consecutive rows from a df, creating a new data frame with the results? The idea is a non-overlapping sliding window approach.
I already can make the average for every n vectors with the code:
ds<-data.frame(do.call(cbind, lapply(seq(1, ncol(df), by = 8), function(idx) rowMeans(df[c(idx, idx + 1)]))))
But now I need a code for averaging in both ways: by column and by row
The data example
df <- data.frame(v1=1:6,V2=7:12,V3=13:18,v4=19:24,v5=25:30,v6=31:36)
Acordingly, for n = 2 I expect to get
df1 <-data.frame(v1 = c(4.5,3.5,5.5),v2 = c(16.5,18.5,20.5),v2=c(28.5,30.5,32.5))
Thank you for answering :)