安排周期对象(ms 函数)不起作用 - R
arrange with period object (ms function) doesn't work - R
我有一个按以下格式 mm:ss 记录的时间,其中分钟值实际上可以大于 59。我已将其解析为 chr
。现在我需要按降序对我的观察结果进行排序,所以我首先创建了一个带有 ms
函数的 time2
变量,并在新变量上使用了 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()
这个时间怎么安排?
我认为将 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)
这是 arrange 的一个已知限制。 dplyr 不支持 S4 对象:https://github.com/tidyverse/lubridate/issues/515
我有一个按以下格式 mm:ss 记录的时间,其中分钟值实际上可以大于 59。我已将其解析为 chr
。现在我需要按降序对我的观察结果进行排序,所以我首先创建了一个带有 ms
函数的 time2
变量,并在新变量上使用了 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()
这个时间怎么安排?
我认为将 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)
这是 arrange 的一个已知限制。 dplyr 不支持 S4 对象:https://github.com/tidyverse/lubridate/issues/515