How to Use xtabs() in R to Calculate Frequencies

Spread the love

The xtabs() function in R is a powerful yet simple tool for generating frequency tables, also known as contingency tables or cross-tabulations. This function is particularly useful when you want to summarize the relationships between two or more categorical variables in your dataset.

Introduction to xtabs( )

At its core, xtabs() provides a convenient way to count the frequency of combinations of factor levels in a dataset. The function is part of the base R package, which means you don’t have to install any additional packages to use it.

Syntax of xtabs( )

The general syntax of the xtabs() function is as follows:

xtabs(formula, data)
  • formula: Specifies the combination of variables you want to summarize. The formula is generally in the form ~ Variable1 + Variable2 + ....
  • data: The data frame containing the variables specified in the formula.

Basic Use of xtabs( )

To start using xtabs(), let’s create a simple example data frame that contains two categorical variables: Gender and Department.

# Create example data frame
example_data <- data.frame(
  Gender = c("Male", "Female", "Male", "Female", "Male"),
  Department = c("HR", "Finance", "HR", "Finance", "IT")

# Generate frequency table
xtabs(~ Gender + Department, data = example_data)

In this example, xtabs() will generate a 2×3 frequency table (2 levels of Gender x 3 levels of Department). Each cell in the table will contain the count of each combination of Gender and Department.

Advanced Usage: More than Two Variables

xtabs() is not limited to two variables; you can use multiple variables in the formula to generate higher-dimensional tables. For instance:

# Create example data frame with an additional variable
example_data_multi <- data.frame(
  Gender = c("Male", "Female", "Male", "Female", "Male"),
  Department = c("HR", "Finance", "HR", "Finance", "IT"),
  AgeGroup = c("Young", "Young", "Old", "Old", "Young")

# Generate frequency table
xtabs(~ Gender + Department + AgeGroup, data = example_data_multi)

Working with Weighted Counts

In some cases, you may not want a simple frequency count but a weighted count. For example, suppose you have an additional variable that represents the weight or importance of each observation.

# Create example data frame with weight variable
example_data_weighted <- data.frame(
  Gender = c("Male", "Female", "Male", "Female", "Male"),
  Department = c("HR", "Finance", "HR", "Finance", "IT"),
  Weight = c(5, 10, 5, 10, 1)

# Generate weighted frequency table
xtabs(Weight ~ Gender + Department, data = example_data_weighted)

Aggregating Additional Variables

The formula in xtabs() can also accommodate aggregating additional variables by using the left-hand side of the formula. For example, if you have a numeric variable and you want to sum it across categories, you can specify it like this:

# Create example data frame with a numeric variable
example_data_aggregate <- data.frame(
  Gender = c("Male", "Female", "Male", "Female", "Male"),
  Department = c("HR", "Finance", "HR", "Finance", "IT"),
  Salary = c(50000, 60000, 55000, 59000, 70000)

# Generate frequency table with sum of Salary
xtabs(Salary ~ Gender + Department, data = example_data_aggregate)

Converting Output to Data Frame

The output of xtabs() is a table object. You can convert it to a data frame for easier manipulation using the function:

# Generate frequency table
result <- xtabs(~ Gender + Department, data = example_data)

# Convert to data frame
result_df <-


The xtabs() function in R offers a versatile and efficient way to generate frequency tables. It can accommodate multiple categorical variables, weighted counts, and additional aggregated variables. Whether you are working in exploratory data analysis or preparing data for statistical modeling, xtabs() can be an invaluable tool for summarizing categorical data.

Posted in RTagged

Leave a Reply