Problem with date format using as.POSIXct function in R

I work with the date format YYYY-mm-ddTHH: MM: SS.000Z (2014-02-05T08: 45: 01.326Z) or have a T separator that separates the date from time and the Z time indicator or Zulu time (UTC). I am trying to save timestamp as a POSIXct class using the following function:

timestamp <- as.POSIXct(strptime(as.character(data$Time), tz = "UTC", "%Y-%m-%d %H:%M:%S"))

I'm currently getting NA. If anyone has any recommendations on how I can incorporate the “T” and “Z” indicators into my conversion, I will be very grateful.

+3
source share
1 answer

You can include characters in a format string:

d <- "2014-02-05T08:45:01.326Z"
timestamp <- strptime(d, tz = "UTC", "%Y-%m-%dT%H:%M:%OSZ")

Note that it is %OSused instead %S, because you have fractional seconds.

+4
source

All Articles