Skip to content

Instantly share code, notes, and snippets.

@mathzero
Last active April 29, 2020 08:15
Show Gist options
  • Save mathzero/84845179b076ad95887d9847607a315a to your computer and use it in GitHub Desktop.
Save mathzero/84845179b076ad95887d9847607a315a to your computer and use it in GitHub Desktop.
Use tableone package to create table one
# Create table 1 ------------------------------------
library(tableone)
myVars <- c("var1","var2") ### create list of variables to include intable
# Make sure any categorical variables are saved as factors
MyData$var2 <- as.factor(MyData$var2)
# package says to pass a list of categorical variables separately but this doesn't seem to work
# passing categorical variables in the main variable list works, so long as they are saved as factors
## Create a TableOne object
tab1 <- CreateTableOne(vars = myVars,
data = MyData,
strata = "Gender") ### strata tells the function to compare all variables across stratified data set by some categorical variable
### save table one object in a format that can be output
tab1Mat <- print(tab1, quote = FALSE, noSpaces = TRUE, printToggle = FALSE, showAllLevels = TRUE)
## Save to a CSV file
write.csv(tab1Mat, file = "path/filename.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment