I would like to replace NA values in a given column (in date format) with a fixed date, if a given condition hold.
In the example below I would like to replace NA values in dt column with "2012-07-01" if condition a==4 holds. I am looking for the simplest way of doing so, either in dplyr or data.table.
> df1 <- data.table(a = c(1, 2, 3, 4),
+ dt = as.Date(c("2012-06-01", "2012-07-01", NA, NA)))
> df1
a dt
1: 1 2012-06-01
2: 2 2012-07-01
3: 3 <NA>
4: 4 <NA>
>Solution :
With data.table
df1[a == 4 & is.na(dt), dt := as.Date("2012-07-01")]
a dt
<num> <Date>
1: 1 2012-06-01
2: 2 2012-07-01
3: 3 <NA>
4: 4 2012-07-01
In Base R for completeness:
df1[df1$a == 4 & is.na(df1$dt), "dt"] <- as.Date("2012-07-01")