Find Unique Combinations of All Elements from Two Vectors in R
Last Updated :
23 Jul, 2025
The number of possible combinations of two vectors can be computed by multiplying all the successive elements of the first vector with the corresponding elements of the second vector. In case, the two vectors have unique elements, the resultant table or data frame formed has m * n elements, where m is the length of the first vector and n is the length of the second vector, respectively. R programming language provides us in-built method as well as external packages to find out the possible pairs easily.
expand.grid() in R can be used to generate a data frame where the rows are all possible unique combinations formed on taking elements from the argument vectors. More than 2 argument vectors can also be specified. The columns can be assigned customized names, otherwise, the row names are automatic. The number of rows in the data frame is equivalent to the total plausible combinations.
Syntax:
expand.grid(vec1...n)
Example:
R
# declaring first integer vector
vec1 <- c(1:3)
# declaring second string vector
vec2 <- c("GeeksForGeeks","CSE")
# creating a data frame of vector1 and vector2 combinations
expand.grid("col1"= vec1,"col2" = vec2)
Output
col1 col2
1 1 GeeksForGeeks
2 2 GeeksForGeeks
3 3 GeeksForGeeks
4 1 CSE
5 2 CSE
6 3 CSE
Method 2: Using tidyr package
The "tidyr" package in the R language can be installed and loaded into the working directory. The crossing() method of this package is used to create a cross joining of the input vectors to generate all the plausible combinations. The names can also be supplied by using named argument listing during function call. The order of appearance of elements in the vector is retained.
Syntax:
crossing(vec1...n)
Example:
R
library("tidyr")
# declaring first integer vector
vec1 <- letters[1:4]
# declaring second string vector
vec2 <- c(8:10)
# creating a data frame of vector1
# and vector2 combinations
crossing(vec1,vec2)
Output
# A tibble:
12 x 2
vec1 vec2
<chr> <int>
1 a 8
2 a 9
3 a 10
4 b 8
5 b 9
6 b 10
7 c 8
8 c 9
9 c 10
10 d 8
11 d 9
12 d 10
Method 3: Using data.table package
The data.table library can be installed and loaded into the working space. CJ method which refers to (C)ross (J)oin of this package can be used to create a data.table formed from the cross product of the vectors. Multiple argument vectors can be supplied to this method. The output is returned to the form of a data.table using this approach.
Syntax: CJ(vec1..n, sorted = TRUE, unique = FALSE)
Parameter :
- vec1.. n : input argument vectors
- sorted : indicator of whether the order of input vectors is to be retained or not.
- unique : indicator of whether only the unique values of the result should be displayed
Example:
R
library("data.table")
# declaring first character vector
vec1 <- letters[1:4]
# declaring second integer vector
vec2 <- c(8:10)
# declaring third integer vector
vec3 <- c(1:2)
# creating a data frame of vector1
# and vector2 combinations
CJ(vec1, vec2, vec3, unique = TRUE)
Output:
vec1 vec2 vec3
1: a 8 1
2: a 8 2
3: a 9 1
4: a 9 2
5: a 10 1
6: a 10 2
7: b 8 1
8: b 8 2
9: b 9 1
10: b 9 2
11: b 10 1
12: b 10 2
13: c 8 1
14: c 8 2
15: c 9 1
16: c 9 2
17: c 10 1
18: c 10 2
19: d 8 1
20: d 8 2
21: d 9 1
22: d 9 2
23: d 10 1
24: d 10 2
Similar Reads
How to find common elements from multiple vectors in R ? In this article, we will discuss how to find the common elements from multiple vectors in R Programming Language. To do this intersect() method is used. It is used to return the common elements from two objects. Syntax: intersect(vector1,vector2) where, vector is the input data. If there are more th
2 min read
Test for Equality of All Vector Elements in R In this article, we will test the equality of all vector elements in R programming language. Method 1: Using variance We can say that all vector elements are equal if the variance is zero. We can find variance by using the var() function Syntax: var(vector)==0 where vector is an input vector This fu
2 min read
How to test if a vector contains the given element in R ? In this article, let's discuss how to check a specific element in a vector in R Programming Language. Method 1: Using loop A for loop can be used to check if the element belongs to the vector. A boolean flag can be declared and initialized to False. As soon as the element is contained in the vector,
3 min read
Element-wise concatenation of string vector in R Element wise concatenation of two vectors means concurrently taking values from two vectors and joining or concatenating them into one. For this paste() function is used in the R programming language. Syntax: paste(vector1,vector2,....,vectorn) Where, vectors are the inputs to paste function Example
2 min read
How to find Index of Element in Vector in R ? In this article, we will discuss How to find the index of element in vector in the R programming language. We can find the index of the element by the following functions - which()match() Method 1: by using which() which() function basically returns the vector of indexes that satisfies the argument
4 min read
Find product of vector elements in R In this article, we will see how to find the product of vector elements in R programming language. Method 1: Using iteration Approach Create dataframeIterate through the vectorMultiply elements as we goDisplay product The following code snippet indicates the application of for loop over decimal poin
2 min read