根据R

时间:2018-05-18 10:56:03

标签: r sorting datetime

我需要在时间戳列中根据时间对数据帧进行排序。

这是我的输入输入:

structure(list(Time.stamp = structure(1:6, .Label = c("05/06/2016 21:13", 
"05/06/2016 22:52", "05/06/2016 22:58", "09/06/2016 22:40", "09/06/2016 22:45", 
"09/06/2016 22:50"), class = "factor")), .Names = "Time.stamp", class = "data.frame", row.names = c(NA, 
-6L))

我想要的输出是

Expected
05/06/2016            21:13
09/06/2016            22:40
09/06/2016            22:45
09/06/2016            22:50
05/06/2016            22:52
05/06/2016            22:58

我试过润滑包装,

data = dmy_hm(data$Time.stamp)   
data2 = arrange(data,f_device_time_new)

但它不起作用。 任何人都可以提供一些建议吗?

1 个答案:

答案 0 :(得分:0)

一种可能的解决方案是:

data = structure(list(Time.stamp = structure(1:6, .Label = c("05/06/2016 21:13", 
"05/06/2016 22:52", "05/06/2016 22:58", "09/06/2016 22:40", "09/06/2016 22:45", 
"09/06/2016 22:50"), class = "factor")), .Names = "Time.stamp", class = "data.frame", row.names = c(NA, 
-6L))

library(dplyr)
library(lubridate)

data %>%
  mutate(Time.stamp = dmy_hm(Time.stamp),
         hour = hour(Time.stamp),
         min = minute(Time.stamp),
         sec = second(Time.stamp)) %>%
  arrange(hour, min, sec) %>%
  select(Time.stamp)

#   Time.stamp
# 1 2016-06-05 21:13:00
# 2 2016-06-09 22:40:00
# 3 2016-06-09 22:45:00
# 4 2016-06-09 22:50:00
# 5 2016-06-05 22:52:00
# 6 2016-06-05 22:58:00

请注意,在您的情况下,您不需要sec列,但我发布了更为通用的解决方案。