I'm dealing with vectors that aren't all dates using the optional = TRUE
.
The existing answers in base R fail when the string vector starts with a none date. Here is my solution based on this answer https://stackoverflow.com/a/46358096/7840119
x <- c("abc", "01.02.2025", "04/05/2026")
Reduce(c, lapply(x, FUN =
function(x){as.Date(x, tryFormats = c("%d.%m.%Y", "%d/%m/%Y"), optional = T)}
))