How to create all combinations with expand_grid() in R

tidyr
tidyr expand_grid()
Learn create all combinations with expand_grid() in r with clear examples and explanations.
Published

April 3, 2026

Introduction

Creating combinations of data is a common task in data analysis, especially when you need to explore all possible pairings between variables or expand datasets for modeling. R provides powerful functions like expand_grid() and expand.grid() that generate all combinations of input values, making it easy to create comprehensive datasets for analysis and visualization.

Loading Required Packages

First, let’s load the tidyverse package which contains the expand_grid() function we’ll be using throughout this tutorial.

library(tidyverse)

The tidyverse provides modern data manipulation tools that work seamlessly together for data analysis tasks.

Basic Combinations with expand_grid()

Let’s start by creating simple vectors and then generating all possible combinations between them.

var1 <- letters[1:5]
var1

This creates a vector with the first 5 lowercase letters. The letters constant in R contains all 26 lowercase letters of the alphabet.

var2 <- LETTERS[1:5]
var2

Similarly, this creates a vector with the first 5 uppercase letters using R’s built-in LETTERS constant.

Now let’s create all possible combinations of these two vectors:

combination_df <- expand_grid(var1 = letters[1:5],
                             var2 = LETTERS[1:5])
combination_df

The expand_grid() function creates a tibble with 25 rows (5 × 5), showing every possible pairing between the lowercase and uppercase letters.

Comparing expand_grid() vs expand.grid()

R also has a base function called expand.grid() that performs similar operations but with different output formatting.

combination_df_base <- expand.grid(var1 = letters[1:5],
                                  var2 = LETTERS[1:5])
combination_df_base

While expand.grid() produces similar results, expand_grid() returns a tibble and generally integrates better with tidyverse workflows.

Working with Data Frames

You can also use expand_grid() to expand existing data frames with additional variables. Let’s create a simple data frame first:

df <- tibble(year = 2021, 
            quarter = paste0("Q", 1:4))
df

This creates a tibble with one year and four quarters. The paste0() function concatenates “Q” with numbers 1-4.

Now let’s expand this data frame to include multiple companies:

expanded_df <- expand_grid(df, 
                          companies = c("GOOG", "MSFT", "NVDA"))
expanded_df

This operation creates 12 rows (4 quarters × 3 companies), duplicating the year and quarter information for each company. This is particularly useful when preparing data for financial analysis or creating templates for data collection.

Practical Applications

The combination functions are especially valuable for: - Creating factorial experimental designs - Generating parameter grids for model tuning - Building comprehensive datasets for simulation studies - Preparing data templates for reporting across multiple categories

Summary

The expand_grid() function provides an efficient way to create all possible combinations of variables in R. Whether you’re working with simple vectors or complex data frames, this function helps you generate comprehensive datasets needed for analysis, modeling, and reporting. Remember to use expand_grid() for tidyverse workflows and expand.grid() when working primarily with base R functions.