You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been working through your very good advanced R book, but think there may be a bug in the rollmean function in the functionals chapter.
I may be wrong, but i'm fairly sure that a centred moving average would be:
rollmean <- function(x, n){
out <- rep(NA, length(x))
offset <- trunc(n/2)
for (i in (offset + 1):(length(x) - n + offset + 1)) {
out[i] <- mean(x[(i - offset):(i + offset)])
}
out
}
The edits are changing upper edge of the range to length(x) - n + offset + 1, and changing the processing range to (i - offset):(i + offset) in the mean step.
Happy to be corrected.
NB: I assign the copyright of this contribution to Hadley Wickham.
mj
The text was updated successfully, but these errors were encountered:
Hi,
I have been working through your very good advanced R book, but think there may be a bug in the
rollmean
function in the functionals chapter.I may be wrong, but i'm fairly sure that a centred moving average would be:
The edits are changing upper edge of the range to
length(x) - n + offset + 1
, and changing the processing range to(i - offset):(i + offset)
in themean
step.Happy to be corrected.
NB: I assign the copyright of this contribution to Hadley Wickham.
mj
The text was updated successfully, but these errors were encountered: