IRNotSmart
IRNotSmart

Reputation: 371

Turning date strings into usable date objects in R

My strings look like:

[1] "Sunday, April 10, 2016" "Saturday, April 16, 2016"

I would like to apply an algorithm in R so they each read something like this and have a Class POSIXlt or POSIXct:

[1] "04/10/2016" "04/16/2016"

I tried to use strptime and as.Date functions, but I just cannot find a good way of doing this automatically without first removing the day of the week up front.

Any and all solutions are appreciated! I know many of you are R gurus out there, and I would very much appreciate your help!

Thank you.

Upvotes: 0

Views: 72

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368241

It is all in help(strptime):

R> d <- c("Sunday, April 10, 2016", "Saturday, April 16, 2016")
R> as.Date(d, "%A, %B %d, %Y")
[1] "2016-04-10" "2016-04-16"
R> 

Note that the result of as.Date() returns a Date object with which you can compute properly: make chanages, add/subtract, compare, convert and format differently for output if needed.

Upvotes: 1

Related Questions