CodexBloom - Programming Q&A Platform

Issue with calculating rolling averages using `zoo::rollapply()` and handling NA values in R

👀 Views: 52 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-24
r zoo rollapply data-cleaning R

Quick question that's been bugging me - I'm trying to calculate rolling averages using the `zoo` package in R, specifically with the `rollapply()` function... However, I'm running into unexpected behavior when it comes to handling NA values in my data. I have a time series data frame with some NA values scattered throughout, and I'm using the following code: ```r library(zoo) # Sample data set.seed(123) df <- data.frame( date = seq(as.Date('2023-01-01'), by='day', length.out=10), value = c(1, NA, 3, 4, NA, 5, 6, NA, NA, 10) ) # Calculating rolling average with a window of 3 roll_avg <- rollapply(df$value, width = 3, FUN = mean, fill = NA, align = 'right') df$roll_avg <- roll_avg ``` The output looks like this: ``` date value roll_avg 1 2023-01-01 1 NA 2 2023-01-02 NA NA 3 2023-01-03 3 NA 4 2023-01-04 4 2.67 5 2023-01-05 NA 3.67 6 2023-01-06 5 4.67 7 2023-01-07 6 5.00 8 2023-01-08 NA 5.67 9 2023-01-09 NA 6.00 10 2023-01-10 10 8.00 ``` I expected that the rolling average would somehow account for the NA values and still return a number when there are at least two non-NA values in the window. Instead, it seems like the presence of any NA in the window leads to a completely NA result. I've tried using the `na.rm = TRUE` option in the `mean()` function within `rollapply()`, but it doesn't seem to affect anything. Is there a way to adjust the `rollapply()` call to get a rolling average that ignores NA values, or do I need to preprocess my data to handle the NA values before applying the rolling function? Any help would be greatly appreciated! For context: I'm using R on Ubuntu. Has anyone else encountered this?