Exploratory Community

How to set NA for Date Column with case_when


When you create a date column based on a condition with case_when, sometimes you want to set NA as a default.
To do so, you can create a Mutate step like this.

The below example creates a new Date column and set today’s date when the country is Argentina and NA for other countries.

case_when(country  == "Argentina" ~ today(), TRUE ~ as.Date(NA))
"Error : must be a `Date` object, not a logical vector Call `rlang::last_error()` to see a backtrace" when trying to set NA for a column with case_when