0

There are a few similar questions I've seen that attempt to answer this question, but none are quite right for my problem.

Converting dates from excel to R

How do I convert dates in this format to a date class in R?

I have dates:

dates <- c("16 Jan 63", "16 Jan 73", "17 Jul 62", "25 Aug 60")

all years begin with "19-"

The obvious solution I thought to use was the as.Date, or strptime functions:

dates <- as.Date(dates, "%d %b %y")
dates
[1] "2063-01-16" "2073-01-16" "2062-07-17" "2060-08-25"

As you can see, the problem is with the year, none are correctly converted.

My next solution was to use strsplit:

dates <- c("16 Jan 63", "16 Jan 73", "17 Jul 62", "25 Aug 60")
new.dates <-  strsplit(dates, " ")
new.dates
[[1]]
[1] "16"  "Jan" "63" 

[[2]]
[1] "16"  "Jan" "73" 
# etc

However the problem begins when I try to paste "19" in front of the year, and then collapse them back into the same character.

new.dates[?] <- paste0("19", new.dates[?])
new.dates[?] <- paste(new.dates[?], collapse = " ")

I have no idea what the dimensions are supposed to be for pasting. Eventually I want to format the dates using:

new.dates <- strptime(new.dates, "%d %b %Y")
new.dates <- as.Date(new.dates)

If anyone has any suggestions on how to do this, or a link to an already answered question, it would be much appreciated.

Community
  • 1
  • 1
Lb93
  • 191
  • 1
  • 12

1 Answers1

0

We can try

as.Date(format(as.Date(dates,"%d %B %y"), "19%y-%m-%d"))
mtoto
  • 23,919
  • 4
  • 58
  • 71