[R] replace NA-values
Patrick Hausmann
patrick.hausmann at uni-bremen.de
Mon Jun 21 13:52:53 CEST 2010
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
More information about the R-help
mailing list