0

I have a chr column that contains time spent on a request in the following format -> 1mos 3w 1d 0h 0m 0s. When I am trying to sort the column and arrange the content in the desc order, it does not work (of course), as the order is based on numerics:

enter image description here

How could I convert this into time column (or smth) to be able to sort it correctly (with 6 months bigger then 7 days? Thank you in advance. I am out of ideas...

Phil
  • 7,287
  • 3
  • 36
  • 66
Katy B
  • 11
  • 2
  • Could you provide an example of how your data looks like? please take a look at this guide: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Guibor Camargo Salamanca Apr 30 '21 at 15:34
  • 1
    Use a regex. `hours <- as.numeric(stringr::str_match(mydata$column, "(\\d+)h")[,2])`. Similarly for the other numbers. Then you can just do `time <- hours * 3600 + mins * 60 + secs` etc. – dash2 Apr 30 '21 at 15:43

0 Answers0