Here is my example:
library(dplyr)
my_df <- data.frame(col_1 = c(1,2,4), col_2 = c('2017-12-1', '2015-11-2', '2011-2-5'))
my_df$col_2 <- as.POSIXct(my_df$col_2)
out <- ifelse(my_df$col_1 ==2, my_df$col_2+ as.difftime(3, units = 'days'), NA)
print(out)
It produces:
NA 1446703200 NA
So coercion happens because of different data type. What NA
datatype should I use to prevent it: NA_date_
or NA_POSIX_
or ...?