[R] using "na.locf" from package zoo to fill NA gaps
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Jul 2 17:34:01 CEST 2012
On Mon, Jul 2, 2012 at 11:17 AM, jeff6868
<geoffrey_klein at etu.u-bourgogne.fr> wrote:
> 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)?
>
Try doing it forwards and backwards and only replacing if they are the same:
library(zoo)
na.locf.ifeq <- function(x) {
ix <- na.locf(x) == na.locf(x, fromLast = TRUE) & is.na(x)
replace(x, ix, na.locf(x)[ix])
}
# test 1
x1 <- c(1, 2, 3, NA, NA, NA, 6, 7, 8, 9)
na.locf.ifeq(x1)
# test 2
x2 <- c(1, 2, 3, NA, NA, NA, 3, 4, 5, 6)
na.locf.ifeq(x2)
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list