I have a folder with many files (read via list.files
and lapply
) that use a mix of two and four digit years. Dates in the 80s and 90s are two digits and dates in the 2000s are four digits (but these are mixed throughout each file, so I can't regex the file name).
Is there a preferred way to handle this? I have the following ad hoc solution.
vec1 <- c("06/30/97", "12/31/99", "01/01/2000", "05/25/2001")
vec2 <- as.POSIXlt(as.Date(vec1, "%m/%d/%Y"))
vec3 <- vec2
vec3$year <- ifelse(vec3$year < 100, vec3$year + 1900, vec3$year)
This seems particularly janky. These cases work, but will this necessarily do the correct %y to %Y adjustment? I am afraid that this will silently fail due to leap years and the like. Thanks!