tidyverse / forcats

🐈🐈🐈🐈: tools for working with categorical variables (factors)
https://forcats.tidyverse.org/
Other
553 stars 126 forks source link

"fct_na_value_to_level" not worked well in gtsummary #350

Closed kkerin closed 10 months ago

kkerin commented 1 year ago

Using "fct_na_value_to_level" can't missing value, when gtsummary reporting categorical variables with missing from the total not the available total. But "fct__explicit_na" worked well:

like such scene below:

library(gtsummary)

gender <- sample(c("Male", "Female", NA), 100, replace = TRUE)
age <- rpois(100,5)
sample1 <-
  data.frame(gender, age) |> 
  dplyr::mutate(
    gender = forcats::fct_explicit_na(gender)
  )

sample1 %>% 
  tbl_summary(statistic = list(all_continuous()~ "{median} ({p25}, {p75})",
                               all_categorical() ~"{n} ({p}%)"),
              digits = all_continuous()~ 2,
              type   = all_categorical() ~ "categorical",
              missing_text = "Missing"
  ) |> 
  as_kable() # export as kable to display on SO

Then I will get: 图片

but if i use "gender = forcats::fct_na_value_to_level(gender)", I will get 图片

hadley commented 10 months ago

It looks like you need fct_na_level_to_value():

library(gtsummary)
#> #BlackLivesMatter

gender <- forcats::fct_na_level_to_value(sample(c("Male", "Female", NA), 100, replace = TRUE))
df <- data.frame(gender)

df |> 
  tbl_summary(missing_text = "Missing") |> 
  as_kable()
Characteristic N = 100
gender
Female 29 (48%)
Male 32 (52%)
Missing 39

Created on 2023-10-31 with reprex v2.0.2