'How do I fix my table when stratifying by two variables using table1 R package?
I am trying to use the table1 package in R to create a table of summary statistics while stratifying by year (2017-2022) and pandemic super-categories (pre-pandemic and during the Covid-19 pandemic). I am able to create a table using Method 1, however, this method required me to remove the "Overall" column (which was actually six columns, one for each year) and I would like to be able to include it (see imgur link 1).
#Method 1
`table1(~ `Disease Type 2` + `Citation Type 2` + `Reference Type 2` + `Guideline Type 2` + `Subject Type 2` + `Becker Model Domain 2` |
Pandemic*Year,
data = Final_Data2,
topclass="Rtable1-shade",
overall = FALSE)`
To try to fix this problem, I adopted the code in Method 2 from Example #2 of this CRAN Documentation (and, in fairness, I don't entirely understand the rationale behind all of the code):
https://cran.r-project.org/web/packages/table1/vignettes/table1-examples.html
#Method 2
#Cleaning Labels
label(Final_Data2$`Disease Type 2`) <- "Disease Type"
label(Final_Data2$`Citation Type 2`) <- "Citation Type"
label(Final_Data2$`Reference Type 2`) <- "Reference Type"
label(Final_Data2$`Guideline Type 2`) <- "Guideline Type"
label(Final_Data2$`Subject Type 2`) <- "Subject Type"
label(Final_Data2$`Becker Model Domain 2`) <- "Becker Model Domain"
#Creating Covid Pandemic Timeline
Final_Data2$Pandemic <- Final_Data2$Year
as_factor(Final_Data2$Pandemic)
Final_Data2$Pandemic <- fct_collapse(Final_Data2$Pandemic,
"Pre-Pandemic" = c("2017", "2018", "2019"),
"Covid-19 Pandemic" = c("2020", "2021", "2022")
)
#Creating stratification by Covid-19 pandemic period for table
Final_Data2$Year <- factor(Final_Data2$Year, levels=c("2017", "2018", "2019", "2020", "2021", "2022"))
strata <- c(split(Final_Data2, Final_Data2$Year),
list(Overall=Final_Data2))
labels <- list(
variables=list(disease=render.varlabel(Final_Data2$`Disease Type 2`),
citation=render.varlabel(Final_Data2$`Citation Type 2`),
reference=render.varlabel(Final_Data2$`Guideline Type 2`),
subject=render.varlabel(Final_Data2$`Subject Type 2`),
becker=render.varlabel(Final_Data2$`Becker Model Domain 2`)),
groups=list("Pre-Pandemic", "Covid-19 Pandemic", ""))
table1(strata, labels, groupspan=c(3, 3, 1))
Now I have run into another problem. When I go to make my table 1 the category levels have been removed (only the variable headings remain) and there are NAs instead of counts in the table itself (see the imgur link 2).
How can I fix either Method 1 to only show one "Overall" column that counts all of my observations or Method 2 to show my results?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
