datetime - convert character string with timezone to date in r -
i have vector of character strings looking this. want convert them dates. characters time-zone posing trouble.
> [1] "07/17/2014 5:01:22 pm edt" "7/17/2014 2:01:05 pm pdt" "07/17/2014 4:00:48 pm cdt" "07/17/2014 3:05:16 pm mdt"
if use: strptime(a, "%d/%m/%y %i:%m:%s %p %z")
[1] na
if omit "%z" time-zone, , use this:
strptime(a, "%m/%d/%y %i:%m:%s %p", tz = "est5edt")
get
[1] "2014-07-17 17:01:22 edt"
since strings contain various time zones - pdt, cdt, edt, mdt , can't default time zones est5edt
. 1 way overcome split vector different vectors each time-zone, remove letters pdt / edt etc. , apply right timezone strptime
- "est5edt" , "cst6cdt" etc. there other way solve this?
if date first part of elements of character vector , followed time, splitting elements whitespaces possibility. if date needed:
dates <- sapply(a, function(x) strsplit(x, split = " ")[[1]][1]) dates <- as.date(as.character(dates), format = "%m/%d/%y") [1] "2014-07-17" "2014-07-17" "2014-07-17" "2014-07-17"
if time needed:
datetime <- sapply(a, function(x) paste(strsplit(x, split = " ")[[1]][1:3], collapse = " ")) datetime <- strptime(as.character(datetime), format = "%m/%d/%y %i:%m:%s %p") [1] "2014-07-17 17:01:22 cest" "2014-07-17 14:01:05 cest"
you can set different timezone using tz
argument here.
Comments
Post a Comment