moodymudskipper
moodymudskipper

Reputation: 47300

Convert to date from unconventional format in R

I have dates formatted as character strings following the format of this example:

"Wednesday 18 May 2016"

Is there a way to convert it into date directly, maybe with as.Date(mystring,someformat) ?

Upvotes: 0

Views: 146

Answers (1)

akrun
akrun

Reputation: 886948

We can remove the Wednesday followed by space with sub and convert to 'Date'

as.Date(sub("^\\S+\\s+", "", str1), "%d %b %Y")
#[1] "2016-05-18"

If we are using lubridate, just use dmy

library(lubridate)
dmy(str1)
#[1] "2016-05-18 UTC"

data

str1 <- "Wednesday 18 May 2016"

Upvotes: 2

Related Questions