I'm having a similar issue to the question listed here lubridate adds a century and I'm not having much success with the answer provided. The issue is that this is returning a list, and when trying to put it into the data frame is returning negative values and erasing the dmy format.
What I'm trying to do is to 1) reverse any inappropriate adding of centuries, 2) return that value as a vector back in the dataframe, and 3) understand exactly what I used to do it.
I am using lubridate 1.9.2, and R version 4.2.3. I have the following:
some_dates = c("1/1/63", "1/1/94", "1/1/65", "1/1/01", "1/1/86", "1/1/61", "1/1/71", "1/1/69", "1/1/86", "1/1/83", "1/1/94", "1/1/57", "1/1/79", "1/1/83", "1/1/01", "1/1/55", "1/1/77", "1/1/77", "1/1/77", "1/1/90")
twenty_later = c("1/1/84", "1/1/04", "1/1/85", "1/1/21", "1/1/06", "1/1/81", "1/1/91", "1/1/89", "1/1/06", "1/1/03", "1/1/14", "1/1/77", "1/1/99", "1/1/03", "1/1/21", "1/1/75", "1/1/97", "1/1/97", "1/1/97", "1/1/10")
df <- data.frame(some_dates, twenty_later)
df <- df |>
mutate(
some_dates_clean = dmy(some_dates),
twenty_later_clean = dmy(twenty_later)
)
Which shows 1/1/57 as 2057 instead of 1957. From the earlier question there was a function:
adjustCentury <- function(d, threshold=1930){
y <- year(d) %% 100
if(y > threshold %% 100) year(d) <- 1900 + y
d
}
but when I apply it
df$some_dates_clean2 <- lapply(df$some_dates_clean, adjustCentury)
It gives the wrong output. If I use the following:
lapply(df$some_dates_clean, adjustCentury)
it creates a list, and I can unlist()
but it returns an unwanted format.
The approach listed here
str_replace(some_dates, '[0-9]+$', '19\\0')
Takes values that should be 2001 and turns them into 1901. I'm not great at regex and while I do understand that the first argument is looking for values between 0-9, I'm not sure how that's interpreted in the second argument 19\\0
even after going to a live demo on regex101.com.
This approach works but it's not clear at all why, according to the poster, this fails in 2057.
future_dates <- year(some_dates) > year(Sys.Date())
year(dates[future_dates]) <- year(dates[future_dates]) - 100
Thanks in advance. I don't have the reputation to comment on these posts and ask follow up questions and the instructions are a bit too opaque for my level of understanding.