I'm trying to use some some ifelse logic within a mutate function to tidy up a column of strings and convert them to dates, depending on the format of the string that is encountered.
I'm finding that lubridate::ymd()
seems to be giving me a different output depending on whether it is within an ifelse()
statement or not, and I'm confused.
Here's a simplified reprex to demonstrate what I'm seeing:
ymd_regex <- "^20[0-9]{2}-[0-9]{1,2}-[0-9]{1,2}$"
grepl(ymd_regex, "2022-02-01")
#> [1] TRUE
lubridate::ymd("2022-02-01")
#> [1] "2022-02-01"
ifelse(grepl(ymd_regex, "2022-02-01"), lubridate::ymd("2022-02-01"), "Error")
#> [1] 19024
dplyr::if_else(grepl(ymd_regex, "2022-02-01"), lubridate::ymd("2022-02-01"), Sys.Date())
#> [1] "2022-02-01"
Created on 2023-06-20 with reprex v2.0.2
I want the "2022-02-01"
date output but I'm getting the 19024
numeric output instead.
Please can someone explain what might be happening?
EDIT - sorry I have just seen from the ifelse help that:
ifelse() strips attributes
I've amended my reprex to show that dplyr::if_else()
gives the output I need (though it throws warnings when used on a mixed vector within mutate())