Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problems with ordering dates in r

Tags:

datetime

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

like image 783
drmariod Avatar asked Jun 08 '26 18:06

drmariod


1 Answers

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

like image 147
Marc in the box Avatar answered Jun 10 '26 07:06

Marc in the box