
Descriptive statistics in R with the table1 package: A guide
Descriptive statistics are an essential part of data analysis as they give you an initial overview of your data. They help to understand important features of the data, such as means, medians, standard deviations and distributions. In R, there are many packages that can calculate these statistics, and one of the most user-friendly packages for this is the table1 package. In this blog post, I will show you how to create descriptive statistics with table1 in R.
Installing and loading the table1 package
Before we start, you need to make sure that the table1 package is installed. If it is not yet installed, you can install it with the following command:
install.packages("table1")
Then load the package with :
library(table1)
Overview of the functionalities of table1
The table1 package is a powerful tool for creating clear and appealing tables for descriptive statistics. It offers the possibility to perform group comparisons and create customizable tables that can be easily integrated into reports.
Simple descriptive statistics with table1
Suppose we have a data set df that contains information about patients, such as age, gender, weight, and cholesterol level. The data set could look like this:
# Beispiel für einen Datensatz
set.seed(123)
df <- data.frame(
Geschlecht = factor(sample(c("Männlich", "Weiblich"), 100, replace = TRUE)),
Alter = rnorm(100, mean = 50, sd = 10),
Gewicht = rnorm(100, mean = 70, sd = 15),
Cholesterin = rnorm(100, mean = 200, sd = 30)
)
Creating a table with descriptive statistics
To create a simple table with descriptive statistics, you can use the following command:
table1(~ Alter + Gewicht + Cholesterin | Geschlecht, data = df)
This command creates a table that summarizes the age, weight and cholesterol level for male and female patients. The ~ symbol separates the dependent variables from the explanatory variables, and the | symbol splits the data by gender.
Adjustment of the output
The table1 package allows you to customize the output very flexibly. For example, you can add user-defined labels and units:
label(df$Alter) <- "Alter (Jahre)"
label(df$Gewicht) <- "Gewicht (kg)"
label(df$Cholesterin) <- "Cholesterin (mg/dL)"
table1(~ Alter + Gewicht + Cholesterin | Geschlecht, data = df)
Adding test statistics
One of the useful functions of table1 is the ability to include test statistics such as the t-test or the chi-square test directly in the table in order to carry out group comparisons. This can be achieved as follows:
table1(~ Alter + Gewicht + Cholesterin | Geschlecht, data = df, overall = "Gesamt", render.missing = NULL, render.categorical = "Frequencies", render.continuous = c(.="Mean (SD)"), test=TRUE)
Here test=TRUE automatically adds appropriate test statistics to evaluate the differences between the groups.
Output as HTML or PDF
The created tables can be easily exported to HTML or PDF documents, which facilitates integration into reports and presentations. To save the table as an HTML file, you can use htmlTable from the htmlTable package:
library(htmlTable)
htmlTable(table1(~ Alter + Gewicht + Cholesterin | Geschlecht, data = df))
For output to PDF, you can work with knitr or rmarkdown to integrate the table directly into an RMarkdown document.
Conclusion
The table1 package in R provides a simple yet powerful way to create and present descriptive statistics. With its flexibility in customization and the ability to add test statistics, it is an excellent tool for the initial exploration of your data. Whether for academic research, clinical trials or business data, table1 helps you communicate your data in an understandable way.



