Open In App

Find Unique Combinations of All Elements from Two Vectors in R

Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Method1 : Using expand.grid() method 

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


Last Updated : 18 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads