Write a function roll.mean(x,k) where x is the input vector and k is the number of observations to use to compute the rolling mean. The function should output a vector of rolling means. Components of the output vector where a rolling mean cannot be computed should contain the value NA.
Post your R function and a test function call using an input vector of all 1’s.
This one took a bit of massaging, and I’m sure there is a more efficient way to code it. The value of k marks the number of observations to compute the rolling mean over, including the first observation.
#==============================================================================
# Function
#==============================================================================
roll.mean = function(x, k){
temp = NULL
for (num.val in 1:(length(x)-k+1)){
temp[num.val] = mean(x[num.val:(num.val+k-1)])
}
return(temp)
}When I do 1:(length(x)-k+1), I add one back in because (for example) 10 - 3 = 7, but 7 to 10 really contains four observations, not three. And we want the rolling mean to cover three observations, which means it stops at observation eight, and takes the arithmetic mean of observations eight, nine, and ten.
Similarly, when I do mean(x[num.val:(num.val+k-1)]), I subtract one out because num.val initalizes at 1, and (for example) 1 + 3 = 4, but we want the rolling mean to cover three observations, not four. Backing one out corrects that.
I called the function on three made-up vectors:
8 with NA.I used the second and third vectors because it was hard to tell if the start and stop points in the function were working correctly with a vector of all 1’s.
One final note - the observation numbers returned in the vectors below might appear a bit confusing: [1] is the first rolling mean value, covering observations 1 through 3 in the input vector. In the third returned vector, NA starts at the sixth rolling mean value, which covers observations 6 through 8.
#==============================================================================
# Function Call
#==============================================================================
# Vector of 1's
df = rep(1, 10)
roll.mean(df, 3)## [1] 1 1 1 1 1 1 1 1
# RNG, Normal
set.seed(123)
df = rnorm(10)
roll.mean(df, 3)## [1] 0.2560184 0.4663464 0.5861681 0.6382870 0.7684230 0.3036400
## [7] -0.4969993 -0.7991920
# RNG, Normal, NA
set.seed(123)
df = rnorm(10)
df[8] = NA
roll.mean(df, 3)## [1] 0.2560184 0.4663464 0.5861681 0.6382870 0.7684230 NA NA
## [8] NA
# Session Info
sessionInfo()## R version 3.3.1 (2016-06-21)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 10586)
##
## locale:
## [1] LC_COLLATE=English_United States.1252
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## loaded via a namespace (and not attached):
## [1] magrittr_1.5 formatR_1.4 tools_3.3.1 htmltools_0.3.5
## [5] yaml_2.1.13 Rcpp_0.12.5 stringi_1.1.1 rmarkdown_1.0
## [9] knitr_1.13.1 stringr_1.0.0 digest_0.6.9 evaluate_0.9