Calculating percent for missing values using gtsummary in Rstudio

一笑奈何 提交于 2021-02-10 08:05:53

问题


My question is a bit similar to this one here.

I have this following codes:

library(gtsummary)
basicvars <- names(isoq) %in% c("homeless_nonself", "test_result")
basictable <- isoq[basicvars]
    # summarize the data
table1 <- tbl_summary(basictable, missing = "always",                              
                      missing_text = "(Missing)",
                      percent = "cell",
                      type = all_dichotomous() ~"categorical"
) %>%
  bold_labels()

############Selecting the order of variables 
basiccompletetable <- basictable %>% select(test_result,homeless_nonself)
mutate(test_result = factor(test_result) %>% fct_explicit_na()) %>%
  
table3 <- tbl_summary(basiccompletetable, #missing = "always", missing_text = "(Missing)",
                      percent = "cell",
                      label = list( 
                        test_result ~ "COVID-19 Test Result",
                        homeless_nonself ~ "Homeless",
                        
                      ),
                      sort = list(
                        test_result ~ "frequency",
                        homeless_nonself ~ "frequency",
                        ),
                      type = list(all_character() ~ "categorical")
) %>%
  modify_spanning_header(starts_with("stat_") ~ "**All**") %>%
  modify_header(label = "**Variable**") %>%  # update the column header
  #add_n() %>%
  bold_labels() %>%
  as_gt() %>%
  gt::tab_source_note(gt::md("*This data is simulated*"))
table3

It spits the output (not the complete output)

I am trying to show the percentages for the missing values. Tried first with test_result. Used this line of code mutate(test_result = factor(test_result) %>% fct_explicit_na()) %>% to what was suggested in the earlier question. However, I am seeing the same table as my output and there are no percentages on the missing values for the variable test_result.

Any suggestions why this is not working? Thanks

来源:https://stackoverflow.com/questions/64884901/calculating-percent-for-missing-values-using-gtsummary-in-rstudio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!