Edu
Edu

Reputation: 903

Parsing a string containing day of the week into date in R

I have a set of dates represented as strings that have the following format:

dates_strings = c("Monday 27 March 2017", "Friday 24 March 2017" , "Wednesday 22 March 2017", "Monday 20 March 2017" , "Wednesday 15 March 2017")

My aim is to parse these strings into date format. I have tried anytime() and something like as.Date(dates_strings, format = "%A% %d %m %Y"). I wonder whether there is a lubridate-type solution similar to dmy() that would consider the day of the week as well.

Upvotes: 1

Views: 241

Answers (2)

akrun
akrun

Reputation: 886938

We can use

as.Date(dates_strings, "%a %d %B %Y")
#[1] "2017-03-27" "2017-03-24" "2017-03-22" "2017-03-20" "2017-03-15"

Upvotes: 1

Dave2e
Dave2e

Reputation: 24069

You need to use "%B" for the month name and not "%m"

dates_strings = c("Monday 27 March 2017", "Friday 24 March 2017" , "Wednesday 22 March 2017", "Monday 20 March 2017" , "Wednesday 15 March 2017")

as.Date(dates_strings, format = "%A %d %B %Y")
[1] "2017-03-27" "2017-03-24" "2017-03-22" "2017-03-20" "2017-03-15"

Upvotes: 3

Related Questions