Calculating time elapsed between two times

Hi guys,

I have a data frame imported from Excel via csv. I have two sets of time periods, bed time and wake time, and I want to create a new column that calculates the elapsed time between these two times, e.g. for the first example, the answer is 8 hours. Do you know how I can do this?

> head(females)
  gender  dob student.height height.dad height.mum bed.time wake.time
1 female 1985         164.00     172.00     155.00 22.30.00  06.30.00
2 female 1990         167.64     177.80     157.48 21.30.00  06.15.00
3 female 1990         165.00     178.00     165.00 22.00.00  07.00.00
4 female 1991         171.00     188.00     169.00 22.00.00  06.45.00
5 female 1991         177.80     182.88     167.64 22.00.00  07.00.00
6 female 1992         162.56     170.18     162.56 21.30.00  07.00.00```

Thanks!

First I replaced the . with : in the times to match the usual American time format, then I converted the character representation of the time to hms objects, then I subtracted the two time (adding 86400 to account for the change in day), and then I converted the difference in seconds back into an hms object.
If the bed.time and wake.time values are not always on different days, the code can be adjusted.

library(hms)
library(dplyr)

DF <- tribble(
~gender,  ~dob, ~student.height, ~height.dad, ~height.mum, ~bed.time, ~wake.time,
'female', 1985,         164.00,     172.00,     155.00, '22.30.00',  '06.30.00',
'female', 1990,         167.64,     177.80,     157.48, '21.30.00',  '06.15.00',
'female', 1990,         165.00,     178.00,     165.00, '22.00.00',  '07.00.00',
'female', 1991,         171.00,     188.00,     169.00, '22.00.00',  '06.45.00',
'female', 1991,         177.80,     182.88,     167.64, '22.00.00',  '07.00.00',
'female', 1992,         162.56,     170.18,     162.56, '21.30.00',  '07.00.00')
DF <- DF |> mutate(across(contains("time"), ~gsub("\\.",":", .x)),
                   across(contains("time"), as_hms),
                   ElapTime = wake.time - bed.time + 86400,
                   ElapTime = as_hms(ElapTime))
DF
#> # A tibble: 6 × 8
#>   gender   dob student.height height.dad height.mum bed.time wake.time ElapTime
#>   <chr>  <dbl>          <dbl>      <dbl>      <dbl> <time>   <time>    <time>  
#> 1 female  1985           164        172        155  22:30    06:30     08:00   
#> 2 female  1990           168.       178.       157. 21:30    06:15     08:45   
#> 3 female  1990           165        178        165  22:00    07:00     09:00   
#> 4 female  1991           171        188        169  22:00    06:45     08:45   
#> 5 female  1991           178.       183.       168. 22:00    07:00     09:00   
#> 6 female  1992           163.       170.       163. 21:30    07:00     09:30

Created on 2022-11-15 with reprex v2.0.2

2 Likes

Thank you so so so much!!

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.