4

I have two character variables in my dataframe start_time and stop_time:

  start_time     stop_time
        <chr>        <chr>   
1     19:5:00     19:11:00 
2    20:33:37     20:34:39
3    20:23:00     20:23:38
4    20:12:00     20:13:00
5    20:00:39     20:00:39

I would like to calculate the difference between start_time and stop_time in terms of minutes with dplyr. My problem is that the two variables are expressed as a character.

expected output:

    diff_time    
        <dbl>         
1           6     
2         1.2    
3        0.38     
4           1   
5        0.39
M--
  • 25,431
  • 8
  • 61
  • 93
nenno lillo
  • 537
  • 1
  • 4
  • 12

3 Answers3

3
library(dplyr)
library(lubridate)

df1 %>% 
  mutate(duration = seconds_to_period(as.numeric(difftime(
                                            strptime(stop_time, "%H:%M:%S"), 
                                            strptime(start_time, "%H:%M:%S"), 
                             units = "secs"))))

#>   start_time stop_time duration
#> 1    19:5:00  19:11:00    6M 0S
#> 2   20:33:37  20:34:39    1M 2S
#> 3   20:23:00  20:23:38      38S
#> 4   20:12:00  20:13:00    1M 0S
#> 5   20:00:39  20:00:39       0S

Data:

read.table(text="  start_time     stop_time
19:5:00     19:11:00 
20:33:37     20:34:39
20:23:00     20:23:38
20:12:00     20:13:00
20:00:39     20:00:39", stringsAsFactors=F, header=T) -> df1
Community
  • 1
  • 1
M--
  • 25,431
  • 8
  • 61
  • 93
  • @M-- can you help me with this? https://stackoverflow.com/questions/58613149/r-and-dplyr-transform-format-variable-dttm-into-posixct – nenno lillo Oct 29 '19 at 18:19
  • 1
    @nennolillo I am taking a look at it. Just one thing; you should provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). This links explains a lot, but in short, you should provide `dput(head(example))` instead of copy/pasting. That would make helping you much easier. (read that thread I shared if you wish, that would help you improve your questions and get help faster). – M-- Oct 29 '19 at 19:04
1

You can use the below snippet to compute time difference and converting to time object:

as.numeric(difftime(strptime(paste("19:11:00"),"%H:%M:%S"),
                    strptime(paste("19:5:00"),"%H:%M:%S")))

output:

[1] 6
ashwin agrawal
  • 1,603
  • 8
  • 16
1

You can try:

library(lubridate)
data_time <- data.frame(start_time = c("19:5:00","20:33:37","20:23:00","20:12:00","20:00:39"),
                    stop_time =   c("19:11:00","20:34:39","20:23:38","20:13:00","20:00:39"), stringsAsFactors = FALSE)

data_time$difference <- difftime(as.POSIXct(data_time$stop_time, format = "%H:%M:%S"), 
                             as.POSIXct(data_time$start_time, format = "%H:%M:%S"), 
                             units = "mins")

Regards,

Alexis

Alexis
  • 2,104
  • 2
  • 19
  • 40