rgtsummary

tbl_summary(): Trouble reporting 0% results for dichotomous variables


I am dealing with an issue passing dichotomous data (1,0) where the '1' values have been filtered out to tbl_summary(). The type should be dichotomous, but I get an error message:

Summary type is "dichotomous" but no summary value has been assigned.

Reprex:

data <- data.frame(group = c("a", "b", "a", "a", "b"),
                   resp_1 = c(1, 1, 0, 1, 1),
                   resp_2 = c(0, 1, 0, 0, 1))

data %>% filter(group == "a") %>% 
  select(starts_with("resp")) %>% 
  tbl_summary(type = everything() ~ "dichotomous")

Not specifying type will give me a categorical response for values where there is no 1 values.

Results not as all dichotomous

I've searched for a solution but haven't found one that will allow "0%" as a dichotomous response. I'm sure there's a straightforward solution.

In the past, I have recoded the 1 and 0 values to "Y"/"N" or some other dichotomous values, but doing so would cause me to rewrite a lot of code. I am hoping to coerce tbl_summary to reporting 0% as a dichotomous result instead of a categorical one like in the attached screenshot.

I am using 'gtsummary' version 2.0.0 and R version 4.1.1.


Solution

  • @Edward is correct that this is a change in gtsummary v2.0. In the past, there has been an issue with unobserved values being incorrectly (e.g. via a typo) assigned as the dichotomous level to summarize. If you want an unobserved level to be summarized dichotomously, you need to explicitly make the level a factor level.

    Below is an example of how I would do this:

    library(gtsummary)
    library(dplyr)
    
    data <- tibble(group = c("a", "b", "a", "a", "b"),
                   resp_1 = c(1, 1, 0, 1, 1),
                   resp_2 = c(0, 1, 0, 0, 1))
    
    data |> 
      filter(group == "a") |> 
      select(starts_with("resp")) |> 
      mutate(across(c(resp_1, resp_2), ~factor(., levels = c(0, 1)))) |> 
      tbl_summary(
        type = everything() ~ "dichotomous",
        value = c(resp_1, resp_2) ~ "1"
      ) |> 
      as_kable()
    
    Characteristic N = 3
    resp_1 2 (67%)
    resp_2 0 (0%)

    Created on 2024-08-20 with reprex v2.1.0