drmariod
drmariod

Reputation: 11762

problems with ordering dates in r

I am trying to order dates in R. I have some strings which look like this

jnk <- c("2016-01-12T10:54:41Z", "2016-01-12T12:40:30Z", "2016-01-12T14:59:22Z", 
         "2016-01-12T15:55:10Z", "2015-03-29T02:56:42Z", "2015-03-29T02:40:56Z")

So I format them with strptime

jnk2 <- strptime(jnk,  "%Y-%m-%dT%H:%M:%SZ")

When I now try to order them, the two 2015 dates will always be at the end...

order(jnk2)

[1] 1 2 3 4 5 6

Am I missing something? I would expect the order to be 6, 5, 1, 2, 3, 4

Upvotes: 3

Views: 149

Answers (1)

Marc in the box
Marc in the box

Reputation: 11995

I had the same issue until I defined a time zone (e.g. tz="GMT"):

jnk <- c("2016-01-12T10:54:41Z", "2016-01-12T12:40:30Z", "2016-01-12T14:59:22Z", 
         "2016-01-12T15:55:10Z", "2015-03-29T02:56:42Z", "2015-03-29T02:40:56Z")

jnk2 <- strptime(jnk,  "%Y-%m-%dT%H:%M:%SZ", tz="GMT")
order(jnk2)

[1] 6 5 1 2 3 4

Upvotes: 8

Related Questions