安排期间对象(ms函数)不起作用 - R.

时间:2018-03-26 17:57:11

标签: r dplyr lubridate

我有以下格式记录时间mm:ss,其中分钟值实际上可以大于59.我已将其解析为chr。现在我需要按降序对观察结果进行排序,因此我首先使用time2函数创建ms变量,并在新变量上使用arrange。但是,安排不起作用,第二栏中的值完全混淆。

library(tidyverse)
library(lubridate)
test <- structure(list(time = c("00:58", "07:30", "08:07", "15:45", "19:30", 
                            "24:30", "30:05", "35:46", "42:23", "45:30", "48:08", "52:01", 
                            "63:45", "67:42", "80:12", "86:36", "87:51", "04:27", "09:34", 
                            "12:33", "18:03", "20:28", "21:39", "23:31", "24:02", "26:28", 
                            "31:13", "43:03", "44:00", "45:38")), .Names = "time", row.names = c(NA, 
                                                                                                 -30L), class = c("tbl_df", "tbl", "data.frame"))
test %>% mutate(time2 = ms(time)) %>% arrange(time2) %>% View()

我如何安排这个时间?

2 个答案:

答案 0 :(得分:1)

我认为将time放在同一个单元然后arrange()更容易。试试这个:

test %>% mutate(time_in_seconds = minute(ms(time) )*60 + second(ms(time))) %>%
  arrange(desc(time_in_seconds)) %>%
  View()

seconds_to_period(minute(ms(test$time))*60 + second(ms(test$time))) # to get right format (with hours)

答案 1 :(得分:1)

这是安排的已知限制。 dplyr不支持S4对象:https://github.com/tidyverse/lubridate/issues/515