Home » How to Find All Unique Combinations of Two Vectors in R

How to Find All Unique Combinations of Two Vectors in R

by Erma Khan

You can use one of the following methods to find all unique combinations of elements from two vectors in R:

Method 1: Use tidyr

library(tidyr)

#find unique combinations of elements from vector1 and vector2
crossing(vector1, vector2)

Method 2: Use data.table

library(data.table)

#find unique combinations of elements from vector1 and vector2
CJ(vector1, vector2, unique=TRUE) 

The following examples show how to use each of these methods in practice.

Example 1: Find Unique Combinations Using tidyr

The following code shows how to find all unique combinations of elements between two vectors in R by using the crossing() function from the tidyr package:

library(tidyr)

#define vectors
region=c('North', 'South', 'East', 'West')
points=c(0, 5, 10)

#display all unique combinations of region and points
crossing(region, points)

# A tibble: 12 x 2
   region points
      
 1 East        0
 2 East        5
 3 East       10
 4 North       0
 5 North       5
 6 North      10
 7 South       0
 8 South       5
 9 South      10
10 West        0
11 West        5
12 West       10

The result is a data frame that displays all unique combinations of elements between the two vectors.

We can see that there are 12 unique combinations.

If you only want to know the number of unique combinations, you can wrap this function with the nrow() function:

library(tidyr)

#define vectors
region=c('North', 'South', 'East', 'West')
points=c(0, 5, 10)

#display number of unique combinations of region and points
nrow(crossing(region, points))

[1] 12

Note that the crossing() function can be used with more than two vectors as well.

Simply provide the names of as many vectors as you’d like to the crossing() function to find the total number of unique combinations.

Example 2: Find Unique Combinations Using data.table

The following code shows how to find all unique combinations of elements between two vectors in R by using the CJ() function from the data.table package:

library(data.table)

#define vectors
region=c('North', 'South', 'East', 'West')
points=c(0, 5, 10)

#display all unique combinations of region and points
CJ(region, points, unique=TRUE)

    region points
 1:   East      0
 2:   East      5
 3:   East     10
 4:  North      0
 5:  North      5
 6:  North     10
 7:  South      0
 8:  South      5
 9:  South     10
10:   West      0
11:   West      5
12:   West     10

The result is a data frame that displays all unique combinations of elements between the two vectors.

Notice that the results from the CJ() function match the results from the crossing() function.

The CJ() function can also be used with more than two vectors as well.

Simply provide the names of as many vectors as you’d like to the CJ() function to find the total number of unique combinations.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Count Unique Values by Group in R
How to Select Unique Rows in a Data Frame in R
How to Count Distinct Values Using dplyr

Related Posts