0

My dataset has 90,000 odd records. date format is as below:

timestamp
2018-01-27T09:01:49Z
2018-01-27T00:04:08Z
2018-01-27T09:04:22Z
2018-01-27T09:04:28Z
2018-01-27T00:07:38Z

I tried

as.Date(recfive$timestamp, "%Y/%m/%d %H:%M:%S")

I am getting all NA

[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Uwe
  • 41,420
  • 11
  • 90
  • 134

2 Answers2

1

The date separators in your strings are "-" and not "/"

Try as.Date(recfive$timestamp, "%Y-%m-%d")

or as.POSIXct(recfive$timestamp, format = "%Y-%m-%dT%H:%M:%SZ") if you want a datetime object

Julien Navarre
  • 7,653
  • 3
  • 42
  • 69
  • Thank you. If I run as.POSIXct I got datetime object, but I can see that few obs are getting omitted. how do I avoid that and include them in my actual dataset – Abhishek BS Feb 09 '18 at 06:53
  • Note that `as.POSIXct()` will ignore the timezone indicator `Z` (*Zulu* = *UTC*) on input and will use the local timezone by default. So the resulting POSIXct objects will be different from the given datetime strings (unless your local timezone is set to *UTC*). – Uwe Feb 12 '18 at 15:34
0

Coercing ISO 8601 standard timestamps to class Date

The timestamps are already in ISO 8601 standard format which is unambiguous. Therefore, no format specification is required when converting to class Date:

as.Date(recfive$timestamp)
#> [1] "2018-01-27" "2018-01-27" "2018-01-27" "2018-01-27" "2018-01-27"

Coercing ISO 8601 standard timestamps to class POSIXct (datetime)

When coercing to a datetime object (class POSIXct), the timezone has to be specified. Although, the timezone is already indicated in the input data by the trailing letter Z (as Zulu = UTC) this is ignored on input, unfortunately.

In my current locale, the local timezone is used which creates a POSIXct object where the timestamps denote different instances in time than specified (during Winter, CET is one hour ahead of UTC).

as.POSIXct(recfive$timestamp)
#> [1] "2018-01-27 CET" "2018-01-27 CET" "2018-01-27 CET" "2018-01-27 CET" "2018-01-27 CET"

So, the timezone has to be specified explicitely:

as.POSIXct(recfive$timestamp, tz = "UTC")
#> [1] "2018-01-27 UTC" "2018-01-27 UTC" "2018-01-27 UTC" "2018-01-27 UTC" "2018-01-27 UTC"

Note that as.POSIXct() has returned a truncated datetime denoting the start of the day.

To get the full datetimes as specified, as.POSIXct() can be called with a format specification:

as.POSIXct(recfive$timestamp, format = "%FT%TZ", tz = "UTC")
#> [1] "2018-01-27 09:01:49 UTC" "2018-01-27 00:04:08 UTC" "2018-01-27 09:04:22 UTC"
#> [4] "2018-01-27 09:04:28 UTC" "2018-01-27 00:07:38 UTC"

Alternatively, there is a number of packages which have specialized in simplified or fast coersion of datetime strings to class POSIXct which all return the same result:

lubridate::ymd_hms(recfive$timestamp)

anytime::anytime(recfive$timestamp, asUTC = TRUE)
anytime::utctime(recfive$timestamp, tz = "UTC")

fasttime::fastPOSIXct(recfive$timestamp, tz = "UTC")

Data

recfive <- data.table::fread(
"timestamp
2018-01-27T09:01:49Z
2018-01-27T00:04:08Z
2018-01-27T09:04:22Z
2018-01-27T09:04:28Z
2018-01-27T00:07:38Z")
Uwe
  • 41,420
  • 11
  • 90
  • 134