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

Reply via email to