Find Unique Combinations of All Elements from Two Vectors in R (2 Examples)

 

In this tutorial, I’ll explain how to find unique combinations of two vectors or arrays in the R programming language.

The article will consist of the following content blocks:

Let’s just jump right in!

 

Example Data

First, we’ll need to create some example data:

vec1 <- c("A", "B", "AA", "CDE")    # Create example vectors
vec2 <- 1:3

As you can see based on the previous R syntax, our example data are two vectors. One of them is numeric, and the other one is a character string.

 

Example 1: Creating Data Containing a Row for Each Unique Combination

Example 1 shows how to construct a data frame consisting of all unique combinations of our two example vectors. For this, we can use the expand.grid function, which is already provided by the basic installation of the R programming language:

expand.grid(vec1, vec2)             # Apply expand.grid function
#    Var1 Var2
# 1     A    1
# 2     B    1
# 3    AA    1
# 4   CDE    1
# 5     A    2
# 6     B    2
# 7    AA    2
# 8   CDE    2
# 9     A    3
# 10    B    3
# 11   AA    3
# 12  CDE    3

Have a look at the previous output of the RStudio console: We have just created a data frame consisting of two columns. The combinations of those two variables are representing all unique combinations of our input vectors.

 

Example 2: Create Unique Combinations Using tidyr Package

Example 2 illustrates how to create a data matrix containing all unique combinations based on the tidyr package. The tidyr package is part of the tidyverse, which is quite popular for the manipulation of data in R.

If we want to use the functions of the tidyr package, we first have to install and load tidyr:

install.packages("tidyr")           # Install & load tidyr
library("tidyr")

Now, we can use the crossing function provided by the tidyr package as shown below:

crossing(vec1, vec2)                # Apply crossing function
# # A tibble: 12 x 2
#    vec1   vec2
#    <chr> <int>
# 1  A         1
# 2  A         2
# 3  A         3
# 4  AA        1
# 5  AA        2
# 6  AA        3
# 7  B         1
# 8  B         2
# 9  B         3
# 10 CDE       1
# 11 CDE       2
# 12 CDE       3

The output of Example 2 is showing the same values as the output of Example 1. However, this time the output is stored in a tibble instead of a data frame.

 

Video, Further Resources & Summary

I have recently released a video on my YouTube channel, which illustrates the R codes of this tutorial. You can find the video below:

 

 

In addition, you could have a look at the related RStudio articles of my homepage. I have published numerous tutorials already:

 

At this point you should have learned how to create a data frame containing all unique combinations of two vectors in the R programming language. If you have any additional questions or comments, let me know in the comments section.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


4 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top