[R] using "na.locf" from package zoo to fill NA gaps
jeff6868
geoffrey_klein at etu.u-bourgogne.fr
Mon Jul 2 17:17:20 CEST 2012
Hi everybody,
I have a small question about the function "na.locf" from the package "zoo".
I saw in the help that this function is able to fill NA gaps with the last
value before the NA gap (or with the next value).
But it is possible to fill my NA gaps according to the last AND the next
value at the same time?
Actually, I want R to fill my gaps with the method of "na.locf" only if the
last value before the gap and the next value after the gap are identical.
Here's an example: imagine this small DF:
df <- data.frame(x1=c(1:3,NA,NA,NA,6:9))
In this case, the last value before NA ("3") and the next value after NA
("6") are different, so I don't want him to fill this gap.
But if I have a DF like this:
df2 <- data.frame(x2=c(1:3,NA,NA,NA,3:6))
The last and next value ("3") are identical, so in this case I want him to
fill my gap with "3" as would do the na.locf function:
na.locf(df2)
But as you understood, I want to do this only if last and next value are
identical. If they're not, I want to keep my NA gap.
Have you any idea how I can do this (maybe something to add to "na.locf" or
maybe another better function to do this)?
Thank you very much!
--
View this message in context: http://r.789695.n4.nabble.com/using-na-locf-from-package-zoo-to-fill-NA-gaps-tp4635150.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list