"fct_na_value_to_level" not worked well in gtsummary
Closed this issue · 1 comments
kkerin commented
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
but if i use "gender = forcats::fct_na_value_to_level(gender)", I will get
hadley commented
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