knitr::opts_chunk$set(echo = TRUE)
This vignette demonstrates the main function of the
table1(). This vignette is current as of
The main parts of the
table1() are below:
table1(.data, ..., splitby, row_wise, test, type, output, format_number, na.rm)
It contains several useful features for summarizing your data:
mediansoption, you can obtain the median and the first quartile/third quantile.
export = "file_name".
To illustrate, we'll walk through the main arguments with an example on some fictitious data.
set.seed(84332) ## Create Fictitious Data containing several types of variables df <- data.frame(a = sample(1:10000, 10000, replace = TRUE), b = runif(10000) + rnorm(10000), c = factor(sample(c(1,2,3,4,NA), 10000, replace=TRUE)), d = factor(sample(c(0,1,NA), 10000, replace=TRUE)), e = trunc(rnorm(10000, 20, 5)), f = factor(sample(c(0,1,NA), 10000, replace=TRUE)))
We will use
df to show these main features of
table1, the ellipses (the
...), are the variables to be summarized that are found in your data. Here, we have
table1(df, a, b, c, d, e)
To get means/count and SD's/percentages by a stratifying variable, simply use the
splitby argument. The splitby can be a quoted variable (e.g.,
"df") or can be a one-sided formula as shown below (e.g.,
table1(df, a, b, c, splitby = ~d)
You can get percentages by rows instead of by columns (i.e., groups) by using the
row_wise = TRUE option.
table1(df, a, b, c, splitby = ~d, row_wise = TRUE)
It is easy to test for bivariate relationships, as in common in many Table 1's, using
test = TRUE.
table1(df, a, b, c, splitby = ~d, test = TRUE)
By default, only the p-values are shown but other options exist such as stars or including the test statistics with the p-values using the
The table can be simplified by just producing percentages for categorical variables. Further, it can be condensed by providing only a reference group's percentages for binary variables and the means and SD's are provided on the same line as the variable name.
table1(df, f, a, b, c, splitby = ~d, test = TRUE, type = c("simple", "condensed"))
If the medians and the interquartile range is desired instead of means and SD's, simply use the
table1(df, f, a, b, c, splitby = ~d, test = TRUE, type = c("simple", "condensed"), second = c("a", "b"))
Several output types exist for the table (all of the
knitr::kable options) including
html as shown below. Others include:
table1(df, a, b, c, splitby = ~d, test = TRUE, output = "html")
For some papers you may want to format the numbers by inserting a comma in as a placeholder in big numbers (e.g., 30,000 vs. 30000). You can do this by using
format_number = TRUE.
table1(df, a, b, c, splitby = ~d, test = TRUE, format_number = TRUE)
In order to explore the missingness in the factor variables, using
na.rm = FALSE does the counts and percentages of the missing values as well.
table1(df, a, b, c, splitby = ~d, test = TRUE, na.rm = FALSE)
Here we do not have any missingness but it shows up as zeros to show that there are none there.
Finally, and very importantly, to make it easier to implement in the tidyverse of packages, a piping option is available. This option can use a
grouped_df object output by
dplyr::group_by() and use the groups indicated there as shown below.
library(dplyr) df %>% filter(f == 1) %>% group_by(d) %>% table1(a, b, c, test = TRUE, type = c("simple", "condensed"))
This includes the ability to use multiple grouping variables. The first value is the first grouping variable, then an underscore, followed by the value of the second grouping variable.
df %>% group_by(d, f) %>% table1(a, b, c, test = TRUE, type = c("simple", "condensed"))
You can also adjust the variable names from within the function as so:
table1(df, "Avar" = a, "Bvar" = b, "Cvar" = c, splitby = ~d, test = TRUE)
This is particularly useful when you adjust a variable within the function:
df %>% group_by(d) %>% table1("A" = factor(ifelse(a > 500, 1, 0)), b, c, test = TRUE)
Here we changed
a to a factor within the function. In order for the name to look better, we can assign a new name, otherwise it would be named something like
As a final note, the
"table1" object can be coerced to a
data.frame very easily:
tab1 <- table1(df, a, b, c, splitby = ~d, test = TRUE) as.data.frame(tab1)
table1 can be a valuable addition to the tools that are being utilized to analyze descriptive statistics. Enjoy this valuable piece of furniture!
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.