I have been trying to implement rollapply
and calculate mean of window length 2 excluding the top 6 rows. My data is as follows
Volume <- c(10406513,14252364,7235783,7235783,5593794,5825159,3887574,2959390, 5060051,5984374,5395609,5750741,6065117,4498997,6712159)
I want to calculate the mean from the 7th value with window length 2. Output should be as follows:
Volume Average
10,406,513
14,252,364
7,235,783
7,235,783
5,593,794
5,825,159
3,887,574
2,959,390
5,060,051
5,984,374
5,395,609 3,423,482
5,750,741 4,009,721
6,065,117 5,522,213
4,498,997 5,689,992
6,712,159 5,573,175
I tried the following code but it's not working
window_length <- 2
mean <- function(Volume){mean(Volume)}
rolling_mean <- function(z, width){rollapply(Volume,lag = 6, width=width,FUN = mean, by.column = FALSE, align = "right")}
roll_mean <- rolling_mean(Volume,window_length)
I am getting the following error:
Error in FUN(data[posns], ...) : unused argument (lag = 6)
Any help on this?