Khiem Ha
Khiem Ha

Reputation: 15

How do I parse a concatinated date and time in R?

The date July, 1, 2016 1:15pm and 43 seconds is given to me as the string 160701131543.

I have an entire column in my data frame of this date time. How should I go about parsing this column into usable data.

Upvotes: 0

Views: 94

Answers (2)

dayne
dayne

Reputation: 7774

You can use the as.POSIXct function and specify the format, in your case the format is year, month, day, hour, minute, second. Read more about formatting date and time data on the ?strptime help page.

as.POSIXct("160701131543", format = "%y%m%d%H%M%S")
[1] "2016-07-01 13:15:43 EDT"

The timezone can be changed with the 'tz' parameter.

Upvotes: 4

akrun
akrun

Reputation: 886948

Here is another option with lubridate. The default tz is "UTC". It can be changed by specifying tz

library(lubridate)
ymd_hms("160701131543")
#[1] "2016-07-01 13:15:43 UTC"

Upvotes: 0

Related Questions