Dear R-experts,
The following R code generates a snippet from data frame I am working with at the moment:
rep1 <- c("20/02/01","23/03/02")
rep2 <- c(NA, "03/05/02")
rep3 <- c("16/04/01",NA)
rep4 <- c(NA,"12/02/03")
data <- data.frame(rep1 = rep1, rep2 = rep2, rep3 = rep3, rep4 = rep4)
The data frame generated by the code looks like this:
rep1 rep2 rep3 rep4
1 20/02/01 <NA> 16/04/01 <NA>
2 23/03/02 03/05/02 <NA> 12/02/03
I would like to rearrange this data frame so it looks like this:
rep1 rep2 rep3 rep4
1 20/02/01 16/04/01 <NA> <NA>
2 23/03/02 03/05/02 12/02/03 <NA>
That is, for every row I would like to replace every NA with the next entry in the row, untill there are only NAs left in the row.
The true data frame consists of many thousand rows, so doing this by hand would mean many late hours in the office.
If anyone could tell me how to do this in R, I would be most grateful!
Thomas