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"

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"

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top