0

I’d like replace NAs in my column with the last date entered for that ID. Below is the example for one ID=1: to convert DATE_old column to DATE_new column below:

DF =
ID       DATE_old       DATE_new
1        1/1/2018       1/1/2018
1        NA             1/1/2018
1        NA             1/1/2018
1        3/1/2018       3/1/2018
1        NA             3/1/2018
2 .....

I tried na.locf() which didn’t work:

DF$DATE_new <- ddply(DF$DATE_old, ~ID, na.locf)
Lukas Würzburger
  • 6,543
  • 7
  • 41
  • 75
Ana
  • 1,516
  • 3
  • 15
  • 26

2 Answers2

0

Please try to have a reproducible example nest time, see reprex.

# sample data (3 groups)
set.seed(42) # for reproducability
dates <- seq.Date(Sys.Date(), Sys.Date() + 4, by = "day")
df <- data.frame(
    id = sample(c(1,2,3), 10, replace = TRUE),
    date = sample(c(dates, NA), 10, replace = TRUE)
)

# check the data.frame
print(df)

# using dplyr
# install.packages("dplyr")
library(dplyr)

df %>%
    group_by(id) %>%
    mutate(new_date = max(date, na.rm = TRUE))
JohnCoene
  • 2,107
  • 1
  • 14
  • 31
  • Thanks, but this replaces all NAs with the last date. I actually want the clock to restart when there is a date and not an NA – Ana Oct 11 '18 at 16:07
0

Based on the example, may be we can use fill to replace the NA with adjacent non-NA preceding value

library(dplyr)
df %>% 
    group_by(id) %>%
    fill(DATE_old)
akrun
  • 874,273
  • 37
  • 540
  • 662