Find Unique Combinations of All Elements from Two Vectors in R - GeeksforGeeks

18 Apr, 2022
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 vectorvec1 <- c(1:3) # declaring second string vectorvec2 <- c("GeeksForGeeks","CSE") # creating a data frame of vector1 and vector2 combinationsexpand.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
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 vectorvec1 <- letters[1:4] # declaring second string vectorvec2 <- c(8:10) # creating a data frame of vector1# and vector2 combinationscrossing(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
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 vectorvec1 <- letters[1:4] # declaring second integer vectorvec2 <- c(8:10) # declaring third integer vectorvec3 <- c(1:2) # creating a data frame of vector1# and vector2 combinationsCJ(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
varshagumber28
Picked
R Vector-Programs
R-Vectors
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Convert Matrix to Dataframe in R