0

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?

MLavoie
  • 9,671
  • 41
  • 36
  • 56

1 Answers1

2

Regarding the error message, the rollapply function does not take a lag argument nor does the mean function that you defined so the error message is reporting an argument that cannot be used.

Overwriting the mean function with your own mean function, as is done in the question, is just asking for trouble. Furthermore, the mean function in the question does nothing that mean itself does not do so it really has no valid reason to be defined. Remove that definition.

Regarding the question itself, there seems to be some discrepancy between the description and the sample output shown. Below, the sample output is assumed to be what is desired.

Here are two alternative solutions.

1) From the sample output it seems that you want to take a window of width 11 and then within that window take the mean of the 7th and 8th values returning a vector the same length as the input. This gives that result.

rollapplyr(Volume, 11, function(x) mean(x[7:8]), fill = NA)
##  [1]      NA      NA      NA      NA      NA      NA      NA      NA      NA
## [10]      NA 3423482 4009721 5522213 5689992 5573175

2) Another way to do it is to replace the first 6 values in Volume with NA and then take the mean of the third and fourth prior values. Note that width= can take a one element list containing a vector of offsets which we use here.

rollapplyr(replace(Volume, 1:6, NA), list(-(3:4)), mean, fill = NA)
##  [1]      NA      NA      NA      NA      NA      NA      NA      NA      NA
## [10]      NA 3423482 4009721 5522213 5689992 5573175
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • An extension to my above question - what if the Volume variable has NAs in between. How to calculate mean with the same conditions ? – haritha madiraju Nov 23 '17 at 08:16