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

jeff6868 geoffrey_klein at etu.u-bourgogne.fr
Thu Oct 18 15:24:55 CEST 2012

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 <-

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.

More information about the R-help mailing list