calculate age and time different in minutes

What are the elegant ways that calculate

  1. age based on admdt and birthdate
  2. time difference in minutes based on sdate/stime and edate/etime?
    df <- data.frame(
    stringsAsFactors = FALSE,
    UBCI = c(1001, 1002, 1003, 1004, 1005),
    admdt = c("02-02-2023","01-01-2023",
    "01-01-2023","01-01-2023","02-02-2023"),
    birthdate = c("05-05-2020","01-01-1990",
    "04-04-1988","01-01-1990","10-01-2001"),
    sdate = c("01-01-2023","05-01-2023",
    "03-02-2023","09-15-2023","10-01-2023"),
    stime = c("1330", "1300", "1300", "1300", "1300"),
    edate = c("01-01-2023","05-01-2023",
    "03-04-2023","09-15-2023","10-01-2023"),
    etime = c("1335", "1400", "1300", "1320", "1300")
    )
    Thanks in advance!

Does this work for you?

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
df <- data.frame(
  stringsAsFactors = FALSE,
  UBCI = c(1001, 1002, 1003, 1004, 1005),
  admdt = c("02-02-2023","01-01-2023",
            "01-01-2023","01-01-2023","02-02-2023"),
  birthdate = c("05-05-2020","01-01-1990",
                "04-04-1988","01-01-1990","10-01-2001"),
  sdate = c("01-01-2023","05-01-2023",
            "03-02-2023","09-15-2023","10-01-2023"),
  stime = c("1330", "1300", "1300", "1300", "1300"),
  edate = c("01-01-2023","05-01-2023",
            "03-04-2023","09-15-2023","10-01-2023"),
  etime = c("1335", "1400", "1300", "1320", "1300")
)
df |> mutate(across(.cols= c("admdt", "birthdate", "sdate", "edate"), mdy)) |> 
  mutate(age = trunc(interval(birthdate, admdt)/years(1)),
         s = paste(sdate, stime), #make character date-time for start
         e = paste(edate, etime), #make character date-time for end
         s = ymd_hm(s), e = ymd_hm(e),
         timeDiff = difftime(e, s, units = "mins"))
#>   UBCI      admdt  birthdate      sdate stime      edate etime age
#> 1 1001 2023-02-02 2020-05-05 2023-01-01  1330 2023-01-01  1335   2
#> 2 1002 2023-01-01 1990-01-01 2023-05-01  1300 2023-05-01  1400  33
#> 3 1003 2023-01-01 1988-04-04 2023-03-02  1300 2023-03-04  1300  34
#> 4 1004 2023-01-01 1990-01-01 2023-09-15  1300 2023-09-15  1320  33
#> 5 1005 2023-02-02 2001-10-01 2023-10-01  1300 2023-10-01  1300  21
#>                     s                   e  timeDiff
#> 1 2023-01-01 13:30:00 2023-01-01 13:35:00    5 mins
#> 2 2023-05-01 13:00:00 2023-05-01 14:00:00   60 mins
#> 3 2023-03-02 13:00:00 2023-03-04 13:00:00 2880 mins
#> 4 2023-09-15 13:00:00 2023-09-15 13:20:00   20 mins
#> 5 2023-10-01 13:00:00 2023-10-01 13:00:00    0 mins

Created on 2023-11-21 with reprex v2.0.2

1 Like

Beautiful! Thank you very much

This topic was automatically closed 7 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.