jakes
jakes

Reputation: 2085

arrange with period object (ms function) doesn't work - R

I have a time recorded in following format mm:ss where the minutes values can actually be greater than 59. I have parsed it as chr. Now I need to sort my observations in a descending order so I firstly created a time2 variable with ms function and used arrange on the new variable. However arranging doesn't work and the values in the second column are totally mixed up.

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()

How can I arrange this times?

Upvotes: 1

Views: 144

Answers (2)

Marcelo
Marcelo

Reputation: 4282

This is a known limitation with arrange. dplyr does not support S4 objects: https://github.com/tidyverse/lubridate/issues/515

Upvotes: 1

nghauran
nghauran

Reputation: 6768

I think it would be easier to just put time in te same unit and then arrange(). Try this:

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)

Upvotes: 1

Related Questions