Home > Enterprise >  tbl_summary() not showing YES/NO levels in R
tbl_summary() not showing YES/NO levels in R

Time:07-07

in data frame below g variable has two levels, however tbl_summary() is not showing the its levels.

data.frame(a=c(0,1,2),
           
           b=c(0,1,2),
           
           f=c("m", "f", "m"),
           
           g = c("Yes", "No", "Yes"),
           
           output = c(0,1,0)) %>%
  
  tbl_summary(by=output)

  a b f   g output
1 0 0 m Yes      0
2 1 1 f  No      1
3 2 2 m Yes      0

enter image description here

I tried following enter image description here

How to identify Yes/No columns "automatically":

names_of_yes_no_columns <- names(Filter(function(x) all(stringr::str_detect(x, "Yes|No")), df))

df |> tbl_summary(by = output, type = list(names_of_yes_no_columns ~ "categorical"))

Update 1: Realizing that this is (almost) the same answer as in the linked post: R gtsummary package doesnt show the factor levels in the summary table. Marking as duplicate.

  • Related