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.
Gabor Grothendieck
2012-Jul-02 15:34 UTC
[R] using "na.locf" from package zoo to fill NA gaps
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
Seems to work very well! Thank you very much Gabor! -- View this message in context: http://r.789695.n4.nabble.com/using-na-locf-from-package-zoo-to-fill-NA-gaps-tp4635150p4635160.html Sent from the R help mailing list archive at Nabble.com.