0

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.

IRNotSmart
  • 351
  • 6
  • 18
  • From the `strptime` help: `%A Full weekday name in the current locale. (Also matches abbreviated name on input.)`. Do you have a specific locale that cause it not working. – Xiongbing Jin Apr 03 '16 at 02:21

1 Answers1

1

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725