I have a character vector with dates, formatted in this way (this is just an example):
x <- c("03.04.30", "02.06.32", "01.11.33", "10.10.31")
and so on. I'm using the as.Date
function as such:
x <- as.Date(x, format = "%d.%m.%Y")
and it's working but it's returning years for me in the 2000s, and not in the 1990s as I would like.
My idea is to simply replace the third instance of the period in each element of x
with .19
using gsub
but I cannot come up with a regular expression to do so and I've looked all over the place online and really cannot figure this out.
I've also tried using the substr
function to replace the 6th character of each element of x
with .19
but it's simply replacing the whole character string. Being able to do either of those effectively would help me!
I know I'm probably overlooking a simpler solution but please help me I'm pulling my hair out trying to figure this out.
You could do:
x <- c("03.04.30", "02.06.32", "01.11.33", "10.10.31")
x <- gsub("(\\d{2})$", "19\\1", x)
as.Date(x, format = "%d.%m.%Y")
To get:
[1] "1930-04-03" "1932-06-02" "1933-11-01" "1931-10-10"
This assumes the data is consistently formatted otherwise you might want to rethink the regular expressions.