0

I have a column with hms, but I would like to plot those times as minutes only, as I am displaying histogram with occurences.

the problem is: I have 10 events happening on 1min 10sec and 3 events at 1min 47sec, but I would like to keep plot them all under 1 minute.

I am plotting the lead time, so I want to display minutes only.

I hope my explanation is clear enough, if not I can write more

Ekatef
  • 1,061
  • 1
  • 9
  • 12
  • 1
    You should edit your question to add [a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) with data and a desired result. – alistaire Apr 18 '18 at 08:12
  • I have a row with numbers, data class is difftime. Instead of minutes and seconds, i want R to display mi minutes only e.g.instead of 1.23 to keep only 1 – Juan Carlos Joaquin Apr 18 '18 at 09:08

2 Answers2

1

If I understand your question properly, you may solve your problem with base R functions like this

library(hms)
test_time <- as.hms(c("10:01:10", "11:01:47",
    "12:34:56", "15:01:47", "16:01:23", "18:01:47"))
as.numeric(format(as.POSIXlt(test_time), "%M"))

or like this

as.POSIXlt(paste(Sys.Date(), test_time))$min

Another option is minutes() function from the lubridate package:

library(lubridate)
minute(test_time)
Ekatef
  • 1,061
  • 1
  • 9
  • 12
0

If you prefer to use dplyr, try this method:

library(hms)
library(dplyr)
c("10:01:10", "11:01:47", "12:34:56", "15:01:47", "16:01:23", "18:01:47") %>% 
    as_hms() %>% 
    as.POSIXlt() %>% 
    format("%H:%M") %>% 
    paste0(., ":00") %>% 
    as_hms()

Which returns:

[1] 10:01:00
[2] 11:01:00
[3] 12:34:00
[4] 15:01:00
[5] 16:01:00
[6] 18:01:00

This process simply strips the seconds values, and replaces them with 00 values. This should achieve the outcome you're looking for.

However, if you include a date, you can use lubridate's round_date() function:

library(lubridate)
c("2020-08-08 10:01:10", "2020-08-08 11:01:47", "2020-08-08 12:34:56", "2020-08-08 15:01:47", "2020-08-08 16:01:23", "2020-08-08 18:01:47") %>% 
    ymd_hms() %>% 
    round_date("minute")

Which returns:

[1] "2020-08-08 10:01:00 UTC"
[2] "2020-08-08 11:02:00 UTC"
[3] "2020-08-08 12:35:00 UTC"
[4] "2020-08-08 15:02:00 UTC"
[5] "2020-08-08 16:01:00 UTC"
[6] "2020-08-08 18:02:00 UTC"
chrimaho
  • 580
  • 4
  • 22