Using Rollmean When There Are Missing Values (Na)

Using rollmean when there are missing values (NA)

From ?rollmean

The default method of ‘rollmean’ does not
handle inputs that contain ‘NA’s. In such cases, use ‘rollapply’
instead.

Using rollmean filtering out NA with threshold

1) Define a function which returns NaN if there are thresh or more NA's in its input and returns mean of the non-NA's otherwise. Then use it with rollapply. Convert that to a data frame if desired using as.data.frame but since the data is entirely numeric leaving it as a matrix may be sufficient.

w <- 5
thresh <- w/2

Mean <- function(x, thresh) if (sum(is.na(x)) > thresh) NaN else mean(x,na.rm=TRUE)
rollapply(df, w, Mean, thresh = thresh, fill = NA)

2) Another possibility is to check if there are more than thresh NA's in each cell and if so return NaN and otherwise return the rolling mean. Again use as.data.frame on the result if a data frame is needed. (1) has the advantage over this one that it only calls roll* once instead of twice.

w <- 5
thresh <- w/2

ifelse(rollsum(is.na(df), w, fill = NA) > thresh, NaN,
rollmean(df, w, na.rm = TRUE, fill = NA))

R and Zoo: moving average with ragged data?

Depending on what you're looking for, What about:

   rollapply(hh, 5, mean, na.rm = TRUE)
[1] 3 4 4 3 3 3 1 1

or

   rollapply(hh, 4, mean, na.rm = TRUE)
[1] 3 3 4 4 3 3 1 1 NaN

rollapply na.rm = TRUE giving 0 values instead of NA's

You can do the following (even though not very nice)

require(zoo)
z <- zoo(c(NA, NA, NA, NA,2, 3, 4, 5, NA))
tmp <- rollapply(z, 3, sum, na.rm = TRUE, align = "right")

tmp[is.na(z)[-2:-1] & tmp == 0] <- NA
tmp

so you assign NA wherever z is na and there is a NA produced by rollapply

which gives you:

> tmp
3 4 5 6 7 8 9
NA NA 2 5 9 12 9

R - Calculate rolling mean of previous k non-NA values

With runner it will be something like mean of 3-elements tail window of non-na values. You can achive the same result with slider

library(runner)
tmp.df <- data.frame(
x = c(NA, 1, 2, NA, 3, 4, 5, NA, NA, NA, 6, 7, NA)
)

# using runner
tmp.df$y_runner <- runner(
x = tmp.df$x,
f = function(x) {
mean(
tail(
x[!is.na(x)],
3
)
)
}
)

# using slider
tmp.df$y_slider <- slider::slide_dbl(
tmp.df$x,
function(x) {
mean(
tail(
x[!is.na(x)],
3
)
)
},
.before = Inf
)

tmp.df

# x y_runner y_slider
# 1 NA NaN NaN
# 2 1 1.0 1.0
# 3 2 1.5 1.5
# 4 NA 1.5 1.5
# 5 3 2.0 2.0
# 6 4 3.0 3.0
# 7 5 4.0 4.0
# 8 NA 4.0 4.0
# 9 NA 4.0 4.0
# 10 NA 4.0 4.0
# 11 6 5.0 5.0
# 12 7 6.0 6.0
# 13 NA 6.0 6.0

rollmean fill NAs with original value

We may use coalesce with the original vector to replace the NA with that corresponding non-NA element from original vector

library(dplyr)
library(zoo)
coalesce(rollmeanr(x, 3, fill = NA), x)

If it is a data.frame

ctd %>%
group_by(station) %>%
mutate(roll_mean_beam = coalesce(rollmeanr(beam_coef,
k = 5, fill = NA), beam_coef))

data

x <- 1:10


Related Topics



Leave a reply



Submit