rgtsummary

Using ITime and gtsummary to display summary statistics for time "%H:%M:%S"


I have hospital admission time that is in the format POSIXct "1899-12-31 13:11:00". I am not interested in the dates of these data/times so I extracted the hours, minutes, and seconds using the lubridate package and formatted them as "%H:%M:%S". I then changed the format of the values using ITime which is a time-of-day class stored as the integer number of seconds in the day. I am converting to this format because I would like to be able to run summary statistics on the time of admission (mean, median, min/max).

Up to this point, everything seems to be working fine. For example, I can use mean(sample_data$HOSP_ADMSN_TIME.x) and R will output "14:11:18" which is exactly what I want. The problem I've having is when I pass HOSP_ADMSN_TIME.x to gtsummary I think it's treating it as a continuous variable and the value it outputs in the summary table is incorrect/doesn't make sense to me. The mean for admission time in gtsummary shows 57,078 instead of the expected "14:11:18".

So my main question is if it's possible for gtsummary to treat these time stamps as integers using ITime so I'm able to calculate summary statistics? Perhaps I am using the wrong method to pass this data along to gtsummary.


Sample Dataset

# A tibble: 10 × 1
   HOSP_ADMSN_TIME.x  
   <dttm>             
 1 2021-04-19 10:00:00
 2 2021-05-02 09:18:00
 3 2021-09-09 15:06:00
 4 2021-08-17 13:10:00
 5 2022-01-18 11:44:00
 6 2021-10-13 20:33:00
 7 2021-05-06 19:09:00
 8 2021-08-04 12:49:00
 9 2021-11-15 11:00:00
10 2021-09-01 19:04:00

Strip dates using Lubridate, then convert to ITime

sample_data$HOSP_ADMSN_TIME.x \<- format(ymd_hms(sample_data$HOSP_ADMSN_TIME.x), "%H:%M:%S")

sample_data$HOSP_ADMSN_TIME.x \<- as.ITime(sample_data$HOSP_ADMSN_TIME.x)

Checking if arithmetic works and gives desired output

mean(sample_data$HOSP_ADMSN_TIME.x)
[1] "14:11:18"

Passing sample data to gtsummary to create summary table

sample_data %\>%
  tbl_summary(
    label = list(HOSP_ADMSN_TIME.x \ ~ "Hospital Admission Time"),
    missing = "ifany",
    missing_text = "Missing",
    type = HOSP_ADMSN_TIME.x \ ~ "continuous2",
    statistic = HOSP_ADMSN_TIME.x \ ~ c("{mean}",
                                        "{median} ({p25}, {p75})",
                                        "{min}, {max}")
  ) %\>%
  bold_labels() %\>%
  modify_header(label \ ~ "**Variable**")

enter image description here


Solution

  • Converting HOSP_ADMSN_TIME.x to time object using chron::times worked for me.

    library(gtsummary)
    
    df$HOSP_ADMSN_TIME.x <- format(as.POSIXct(df$datetime), "%H:%M:%S")
    df$HOSP_ADMSN_TIME.x <- chron::times(df$HOSP_ADMSN_TIME.x)
    
    df %>% 
      tbl_summary(
        include = c("HOSP_ADMSN_TIME.x"),
        label = list(HOSP_ADMSN_TIME.x ~ "Hospital Admission Time"),
        type = list(HOSP_ADMSN_TIME.x ~ "continuous2"),
        statistic = list(HOSP_ADMSN_TIME.x ~ c(
          "{mean}",
          "{median} ({p25}, {p75})",
          "{min}, {max}"
          )
        )
      )
    

    output

    enter image description here

    data

    df <- read.table(text="2021-04-19 10:00:00
    2021-05-02 09:18:00
    2021-09-09 15:06:00
    2021-08-17 13:10:00
    2022-01-18 11:44:00
    2021-10-13 20:33:00
    2021-05-06 19:09:00
    2021-08-04 12:49:00
    2021-11-15 11:00:00
    2021-09-01 19:04:00", sep="\n", col.names="datetime")