I need to convert the fctr format for column dob, such as 4/11/64, in Marriage build-in data set to date format using lubridate. I tried many functions, strptime, as_date etc, but the 4/11/64 change to year 2064, which is not correct for a person who were born in 1964. Is there a function that automatically converts the year to 19xx? Or I have to put the leading "19" in character format first before converting?
I think you have to add "19" yourself, unless you want to use hydrostats::four.digit.year
:
hydrostats::four.digit.year(dmy("4/11/64"), year=1900)
(the function is only a few lines long, so you could just copy it if you didn't want to depend on the package)
function (x, year = 1968) {
n <- as.numeric(strftime(x, format = "%y"))%%100
Y <- ifelse(n > year%%100, 1900 + n, 2000 + n)
return(Y)
}
Otherwise, you're stuck with the POSIX standard. "%y"
is the standard tag for converting two- (or one-) digit years, and from ?strptime
:
‘%y’ Year without century (00-99). On input, values 00 to 68 are prefixed by 20 and 69 to 99 by 19 - that is the behaviour specified by the 2018 POSIX standard, but it does also say ‘it is expected that in a future version the default century inferred from a 2-digit year will change’.
The standard itself is available here:
If century is not specified, then values in the range [69,99] shall refer to years 1969 to 1999 inclusive, and values in the range [00,68] shall refer to years 2000 to 2068 inclusive.
See also: why strptime for two digit year for 69 returns 1969 in python?