info@statistiknachhilfe.ch

+41 (0)56 530 01 55

logologologo
  • Offers
    • Where we help
    • Overview
    • Correction of your work
  • Bachelor & Master Theses
  • Tutoring
    • Formulation of hypotheses
    • Help with hypothesis testing
    • Interpretation of hypothesis tests
    • Support with homework
    • Exam preparation
    • Preparation for your studies
    • Tutoring during lectures
  • Data analysis
    • Support with data analysis
    • Review of a data analysis
    • Support for data visualization
    • Preparation for theses
    • Data analysis for final papers
  • For companies
  • About us
  • Contact
  • DE
  • EN
  • Offers
    • Where we help
    • Overview
    • Correction of your work
  • Bachelor & Master Theses
  • Tutoring
    • Formulation of hypotheses
    • Help with hypothesis testing
    • Interpretation of hypothesis tests
    • Support with homework
    • Exam preparation
    • Preparation for your studies
    • Tutoring during lectures
  • Data analysis
    • Support with data analysis
    • Review of a data analysis
    • Support for data visualization
    • Preparation for theses
    • Data analysis for final papers
  • For companies
  • About us
  • Contact
  • DE
  • EN
  • Offers
    • Where we help
    • Overview
    • Correction of your work
  • Bachelor & Master Theses
  • Tutoring
    • Formulation of hypotheses
    • Help with hypothesis testing
    • Interpretation of hypothesis tests
    • Support with homework
    • Exam preparation
    • Preparation for your studies
    • Tutoring during lectures
  • Data analysis
    • Support with data analysis
    • Review of a data analysis
    • Support for data visualization
    • Preparation for theses
    • Data analysis for final papers
  • For companies
  • About us
  • Contact
  • DE
  • EN
August 15, 2024 by Kevin general 0 comments

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.

PREV
NEXT

Related Posts

10 March 2022
Interpret regression coefficients correctly
Read More
February 27, 2024
Linear Mixed Models vs. OLS Models: A Comparison and Selection Guide
Read More
February 26, 2024
How do you check the condition of homogeneity of variance (homoscedasticity) for the linear regression model in R and SPSS?
Read More

Do you have questions about statistics? Do you need help with your master or bachelor thesis?

We'll help you!

Address:
Höhenweg 270, 5046 Walde
Opening hours:
9:00 am - 6:00 pm
Phone:
056 530 01 55
E-Mail:
info@statistiknachhilfe.ch

Offer

  • Bachelor & Master Theses
  • Offers & Prices
  • Tutoring Bundles
  • Correction of your work
  • Preparation for theses
  • Exam preparation
  • Preparation for your studies
  • Support with homework

About us

  • About us
  • Contact
  • Offers & Prices
  • Blog
  • Become a part of our team!
  • Where we help

Legal matters

  • AGB
  • Contact
  • Privacy policy

Partner

  • datenschutzkonform.ch
  • statisticscenter.ch