R as.POSIXct get NA

Why did I get all neural networks when converting timestamps below using as.POSIXct?

> head(tmp$timestamp_utc)
[1] Fri Jul 03 00:15:00 EDT 2015 Fri Jul 03 00:45:00 EDT 2015 Fri Jul 03 01:15:00 EDT 2015 Fri Jul 03 01:45:00 EDT 2015 Fri Jul 03 02:15:00 EDT 2015
[6] Fri Jul 03 02:45:00 EDT 2015
> tmp$timestamp_utc<- as.POSIXct(tmp$timestamp_utc, "%m/%d/%Y %H:%M:%S", tz="GMT")
> head(tmp$timestamp_utc)
[1] NA NA NA NA NA NA

      

UPDATE: 1. The original problem is the wrong format, which was pointed out by some friends below. The correct format will not generate NA 2. Then I find out that EDT was not converted to GMT, although "GMT" is added to asPOSIXCT. Using with_tz solves this problem.

> as.POSIXct("Fri Jul 03 00:15:00 EDT 2015", format="%a %b %d %H:%M:%S EDT %Y", tz="GMT")
[1] "2015-07-03 00:15:00 GMT"
> with_tz(as.POSIXct("Fri Jul 03 00:15:00 EDT 2015", format="%a %b %d %H:%M:%S EDT %Y"),"GMT")
[1] "2015-07-03 04:15:00 GMT"

      

+3


source to share


1 answer


I believe this is the format you want:

as.POSIXct("Fri Jul 03 00:15:00 EDT 2015", format="%a %b %d %H:%M:%S EDT %Y", tz="GMT")

Anyone to answer your specific question and echo the comments, it didn't work because your format argument was not defined correctly.



Edit: This is a bit more than the originally asked question, but you can convert the time based on the timezone change as follows (supporting question here ):

time <- "Fri Jul 03 00:15:00 EDT 2015"
format1 <- "%a %b %d %H:%M:%S EDT %Y"
time2 <- as.POSIXct(time, format=format1, tz="EST")
attr(time2, "tzone") <- "GMT"
time2
[1] "2015-07-03 05:15:00 GMT"

      

+4


source







All Articles