79552382

Date: 2025-04-03 08:37:58
Score: 1
Natty:
Report link

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)}
))

Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Lukas Lohse