Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rolling loop (functionals) #320

Closed
mcooganj opened this issue Jan 25, 2014 · 1 comment
Closed

rolling loop (functionals) #320

mcooganj opened this issue Jan 25, 2014 · 1 comment

Comments

@mcooganj
Copy link

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:

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

@hadley
Copy link
Owner

hadley commented Feb 5, 2014

Makes sense - thanks!

@hadley hadley closed this as completed in cc47d68 Feb 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants