Home > other >  Converting string vector to date
Converting string vector to date

Time:04-11

I have below vector of string

dates = c('1997 Jan- 6', '1997 Jan-13')

I wish I could have a generic approach so that I can get a date vector from this string.

Is there any straightforward way to do this?

Thanks for your insight

CodePudding user response:

We can use ymd

library(lubridate)
ymd(dates)
[1] "1997-01-06" "1997-01-13"

Or with base R

 as.Date(dates, format = '%Y %b-%d')
[1] "1997-01-06" "1997-01-13"
  • Related