Alternative to expand.grid Function without Duplicates in R (Example Code)
In this tutorial, I’ll explain how to get the output of the expand.grid function without duplicates in the R programming language.
Creation of Example Data
my_vec <- letters[1:4] # Example values my_vec # [1] "a" "b" "c" "d" |
my_vec <- letters[1:4] # Example values my_vec # [1] "a" "b" "c" "d"
Example: Non-Redundant Alternative to expand.grid() Function Using combn()
combn(my_vec, 2) # Unique combinations # [,1] [,2] [,3] [,4] [,5] [,6] # [1,] "a" "a" "a" "b" "b" "c" # [2,] "b" "c" "d" "c" "d" "d" |
combn(my_vec, 2) # Unique combinations # [,1] [,2] [,3] [,4] [,5] [,6] # [1,] "a" "a" "a" "b" "b" "c" # [2,] "b" "c" "d" "c" "d" "d"