I have a datetime string like so:
> mydatetime
[1] "2012-01-07 14:53:52 EST" "2012-01-07 07:57:03 EST"
[3] "2012-01-07 17:42:28 EST" "2012-01-08 10:28:35 EST"
[5] "2012-01-08 10:37:22 EST" "2012-01-09 08:12:00 EST"
[7] "2012-01-09 08:11:44 EST" "2012-01-09 17:45:24 EST"
[9] "2012-01-09 14:28:22 EST" "2012-01-09 13:14:38 EST"
I’d like to separate the date and time into separate objects. I can get date successfully as:
> as.Date(mydatetime)
[1] "2012-01-07" "2012-01-07" "2012-01-07" "2012-01-09" "2012-01-08"
[6] "2012-01-08" "2012-01-08" "2012-01-09" "2012-01-09" "2012-01-09"
How can I extract the time portion? My goal is to plot date on the y-axis and time on the x-axis, to show a timelime of each day. Seems like the plot function requires special formats, so strings won’t work here. Any recommendations on timeline-esque plotting functions are appreciated as well. Thanks!
UPDATE
Simon’s answer below worked for me. However, I had to pull the date substring from mydatetime first, then use as.Date.
date <- as.Date(substr(mydatetime, 0,10))
For some reason it was five hours off. For example, 2012-01-09 19:05:21 EST would be converted to 2012-01-10. My timezone settings seem fine, and putting that string into as.Date directly gave 2012-01-09. No problem, I’m happy with this solution. Thanks.
There are several ways — either you treat it as a string:
or you compute on the dates:
There is a subtle difference in the resulting plot ranges.