'Show true unknown count based on question logic
I'm working with surveys where if a specific answer is provided, a follow-up question is given. For example, if the answer to q1 is "yes" then q2 is asked. However, if I add both questions to the same table, the Unknown count for q2 is 5 but should be 0. Unfortunately, I do have missing data and want to show that. How do I only show the q2 Unknown count for those who have q1=="yes" & q2==NA?
library(tidyverse)
library(knitr)
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.6.0'
tibble::tribble(
  ~q1,   ~q2,
  "yes", "yes",
  "yes", "yes",
  "yes",  "no",
  "yes",  "no",
  "yes",  "no",
  "no",    NA,
  "no",    NA,
  "no",    NA,
  "no",    NA,
  "no",    NA
) %>% tbl_summary() %>% as_kable()
| Characteristic | N = 10 | 
|---|---|
| q1 | 5 (50%) | 
| q2 | 2 (40%) | 
| Unknown | 5 | 
Created on 2022-05-06 by the reprex package (v2.0.1)
Solution 1:[1]
If you need a different denominator, the best thing to do is make two tables and stack them. Example below!
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.6.0'
tbl1 <- trial %>% tbl_summary(include = response)
tbl2 <-
  trial %>% 
  dplyr::filter(response == 1) %>%
  tbl_summary(
    include = stage,
    label = stage ~ "Stage Among Responders",
    statistic = stage ~ "{n} / {N} ({p}%)"
  )
tbl <- tbl_stack(list(tbl1, tbl2))
#> ? Column headers among stacked tables differ. Headers from the first table are
#> used. Use `quiet = TRUE` to supress this message.
Created on 2022-05-06 by the reprex package (v2.0.1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | Daniel D. Sjoberg | 
