I am trying to order time stamps with dplyr and lubridate but I'm not getting my expected ordering.
foo <- data.frame(time = ymd_hms(c("2016-08-31 13:40:00", "2016-08-31 06:40:00", "2016-08-31 10:40:00")),
expected_order = c(3,1,2))
foo %>% mutate(dplyr_ordered = order(time))
You confused what the
rank do, from
order returns a permutation which rearranges its first argument into ascending or descending order.
order does not return a rank of the actual value but an index vector which can be used to sort the vector, compare the following result:
foo %>% mutate(dplyr_order = order(time), dplyr_rank = rank(time)) # time expected_order dplyr_order dplyr_rank # 1 2016-08-31 13:40:00 3 2 3 # 2 2016-08-31 06:40:00 1 3 1 # 3 2016-08-31 10:40:00 2 1 2
The result from
rank is what you are expecting. The result from
order tells you that the second element in time is smallest, followed by the third element and the first element is the largest.