# [R] filling NA gaps according to previous data mean and following data mean

Thu Oct 18 15:54:32 CEST 2012

```Hello,

The function below fails if any of the followind conditions is met.

1. The first value in the vector is NA
2. There are less than n = 5 (or 500) values before/after the
first/last, respectively, NA.

fun <- function(x, n){
na <- is.na(x)
rna <- rle(na)
sna <- cumsum(rna\$lengths)
for(i in which(rna\$values)){
prev <- x[(sna[i - 1] - n + 1):sna[i - 1]]
aftr <- x[(sna[i] + 1):(sna[i] + n)]
if(mean(prev) < 0 && mean(aftr) < 0) x[na] <- 0
}
x
}

fun(DF\$data, n = 5)

Hope this helps,

Em 18-10-2012 14:24, jeff6868 escreveu:
> Hi everybody,
>
> I have a little problem about filling some gaps of NAs in my data.
>
> These gaps are between nearly constant data (temperature under snow). Here's
> a fake example to illustrate how it looks like approximately:
>
> DF <-
> data.frame(data=c(-0.51,-0.51,-0.48,-0.6,-0.54,-0.38,-0.6,-0.42,NA,NA,NA,NA,NA,NA,NA,
> -0.25,-0.41,-0.5,-0.5,-0.35,-0.7,-1,-0.87))
>
> I would like to replace my NAs with "0" with this condition:
> Fill the gap with "0" if the mean of the 5 previous values before the gap
> (NA) is under 0°C, AND if the mean of the 5 following values after the gap
> (NA) is also under 0°C (actually it's not the 5 previous and following
> values in my real data, it's my 500 previous and following values, but let's
> juste take the 5 ones in my example).
>
> I think that the nearest function for doing this is the "na.locf" function
> of the package zoo, but it does not really do what I want.
>
> Can somebody help me to resolve this?
> Thanks a lot guys!
>
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/filling-NA-gaps-according-to-previous-data-mean-and-following-data-mean-tp4646613.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help