Table Output

# Load libraries
library(gt)
## Warning: package 'gt' was built under R version 4.4.3
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Apply asterisk if p < 0.05
add_asterisk <- function(x) {
  if (x == "") return("")
  num <- as.numeric(x)
  if (!is.na(num) && num < 0.05) paste0(x, "*") else x
}

# Chi-Square and N-1 with asterisks
chi_raw <- c("0.0467", "", "", "0.0103", "", "", "0.2927", "", "",
             "0.84795", "", "", "0.30874", "", "", "0.92760", "", "",
             "0.16625", "", "", "0.3699", "", "", "0.5392816", "", "",
             "0.90532", "", "", "0.892172", "", "", "0.3038187", "", "",
             "0.08246", "", "", "0.1374581", "", "", "0.09888179", "", "")
chi_vals <- sapply(chi_raw, add_asterisk)

n1_raw <- c("0.1197672", "0.01740435", "0.1936163", "0.01964378", "0.001734283", "0.1842825",
            "0.1853195", "0.7633065", "0.1980888", "0.6097324", "0.8985325", "0.661362",
            "0.4219239", "0.1499462", "0.3347276", "0.7009935", "0.7991386", "0.8745464",
            "0.4164268", "0.4367106", "0.06070303", "0.2900287", "0.4231647", "0.7576573",
            "0.5407487", "0.8036897", "0.2862789", "0.6734992", "0.687283", "1",
            "0.6440702", "0.6944755", "0.9496214", "0.3483889", "0.7144906", "0.1230225",
            "0.04296093", "0.02452464", "0.620654", "0.6382581", "0.2360194", "0.04735505",
            "0.03337852", "0.09149634", "0.5163966")
n1_vals <- sapply(n1_raw, add_asterisk)

data <- tibble(
  Question = c("Q4) Commute distance", "", "", "Q6) Impact on smoky days", "", "",
               "Q7) Kids affected outdoors", "", "", "Q8) Kids affected indoors", "", "",
               "Q9a) Event notification sources", "", "", "Q9b) How to know event is over", "", "",
               "Q10a) Wildfire smoke alerts", "", "", "Q10b) End of wildfire event info", "", "",
               "Q11a) Indoor air monitor", "", "", "Q11c) Outdoor air monitor", "", "",
               "Q12) Protecting kids' health", "", "", "Q14) Best wildfire info sources", "", "",
               "Q15) Govt. support actions", "", "", "Q16) What do you do if it is a smoky day outdoors?", "", "",
               "Q18) Building changes for smoke", "", ""),
  Section = rep(c("B vs C", "B vs D", "C vs D"), 15),
  `Chi Square Test` = chi_vals,
  `Fisher Test` = c("0.04482", "", "", "0.006865", "", "", "0.2876", "", "", "0.863", "", "",
                    "0.3183", "", "", "0.9644", "", "", "0.1628", "", "", "0.3848", "", "",
                    "0.5567905", "", "", "1", "", "", "0.913", "", "", "0.3141478", "", "",
                    "0.05901", "", "", "0.1346723", "", "", "0.09734618", "", ""),
  `N-1 Proportional P-Value` = n1_vals
)

styled_table <- data %>%
  gt() %>%
  tab_header(title = md("**Table 1: (Chi-Square Test, Fisher Test, N-1 Two Proportions)**")) %>%
  tab_spanner("Statistical Tests", c("Chi Square Test", "Fisher Test")) %>%
  tab_spanner("N-1 Proportional Analysis", c("Section", "N-1 Proportional P-Value")) %>%
  cols_label(
    Question = "Question",
    Section = "Section",
    `Chi Square Test` = "Chi Square Test",
    `Fisher Test` = "Fisher Test",
    `N-1 Proportional P-Value` = "N-1 Proportional P-Value"
  ) %>%
  cols_align(align = "center") %>%
  tab_style(
    style = list(cell_text(align = "left", weight = "normal")),
    locations = cells_body(columns = Question)
  ) %>%
  tab_style(
    style = cell_borders(sides = "top", color = "black", weight = px(2)),
    locations = cells_body(rows = seq(1, 45, by = 3))
  ) %>%
  tab_style(
    style = cell_borders(sides = "bottom", color = "black", weight = px(2)),
    locations = cells_body(rows = seq(3, 45, by = 3))
  ) %>%
  tab_options(data_row.padding = px(3)) %>%
  tab_source_note(
    source_note = md("_* indicates p-value < 0.05 (statistically significant based on Chi-Square Test or N-1 Proportional Test)._")
  ) %>%
  tab_source_note(
    source_note = md("<em>Scale:</em>  <strong>p-value < 0.05</strong>: Significant relationship.  <strong>p-value ≥ 0.05</strong>: No significant relationship.")
  ) %>%
  tab_source_note(
    source_note = md("<em>Key:</em>  ● Significant  ● Not Significant")
  )

styled_table
Table 1: (Chi-Square Test, Fisher Test, N-1 Two Proportions)
Question
N-1 Proportional Analysis
Statistical Tests
Section N-1 Proportional P-Value Chi Square Test Fisher Test
Q4) Commute distance B vs C 0.1197672 0.0467* 0.04482
B vs D 0.01740435*
C vs D 0.1936163
Q6) Impact on smoky days B vs C 0.01964378* 0.0103* 0.006865
B vs D 0.001734283*
C vs D 0.1842825
Q7) Kids affected outdoors B vs C 0.1853195 0.2927 0.2876
B vs D 0.7633065
C vs D 0.1980888
Q8) Kids affected indoors B vs C 0.6097324 0.84795 0.863
B vs D 0.8985325
C vs D 0.661362
Q9a) Event notification sources B vs C 0.4219239 0.30874 0.3183
B vs D 0.1499462
C vs D 0.3347276
Q9b) How to know event is over B vs C 0.7009935 0.92760 0.9644
B vs D 0.7991386
C vs D 0.8745464
Q10a) Wildfire smoke alerts B vs C 0.4164268 0.16625 0.1628
B vs D 0.4367106
C vs D 0.06070303
Q10b) End of wildfire event info B vs C 0.2900287 0.3699 0.3848
B vs D 0.4231647
C vs D 0.7576573
Q11a) Indoor air monitor B vs C 0.5407487 0.5392816 0.5567905
B vs D 0.8036897
C vs D 0.2862789
Q11c) Outdoor air monitor B vs C 0.6734992 0.90532 1
B vs D 0.687283
C vs D 1
Q12) Protecting kids' health B vs C 0.6440702 0.892172 0.913
B vs D 0.6944755
C vs D 0.9496214
Q14) Best wildfire info sources B vs C 0.3483889 0.3038187 0.3141478
B vs D 0.7144906
C vs D 0.1230225
Q15) Govt. support actions B vs C 0.04296093* 0.08246 0.05901
B vs D 0.02452464*
C vs D 0.620654
Q16) What do you do if it is a smoky day outdoors? B vs C 0.6382581 0.1374581 0.1346723
B vs D 0.2360194
C vs D 0.04735505*
Q18) Building changes for smoke B vs C 0.03337852* 0.09888179 0.09734618
B vs D 0.09149634
C vs D 0.5163966
* indicates p-value < 0.05 (statistically significant based on Chi-Square Test or N-1 Proportional Test).
Scale: p-value < 0.05: Significant relationship. p-value ≥ 0.05: No significant relationship.
Key: ● Significant ● Not Significant