[R] replace NA-values

jim holtman jholtman at gmail.com
Mon Jun 21 13:56:16 CEST 2010


try 'na.locf' in the zoo package

On Mon, Jun 21, 2010 at 7:52 AM, Patrick Hausmann
<patrick.hausmann at uni-bremen.de> wrote:
> Dear list,
>
> I'm trying to replace NA-values with the preceding values in that column.
> This code works, but I am sure there is a more elegant way...
>
> df <- data.frame(id = c("A1", NA, NA, NA, "B1",
>                 NA, NA, "C1", NA, NA, NA, NA),
>                 value = c(1:12))
>
> rn     <- c(rownames(df[!is.na(df$id),]), nrow(df)+1)
> rn     <- diff(as.numeric(rn))
> df$id2 <- rep(levels(df$id), rn)
>
> thanks for any help
> Patrick
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list