Answers for "create all unique combinations between elements of a vector R"

R
0

create all unique combinations between elements of a vector R

#Create unique pairwise permutations between elements of a vector
install.packages("RcppAlgos") #only run it once

x <- c("Red", "Blue", "Green", "Yellow")
library(RcppAlgos)
comboGeneral( names(table(x)) , m=2, freqs = table(x))

#Output:
     [,1]    [,2]    
[1,] "Blue"  "Green" 
[2,] "Blue"  "Red"   
[3,] "Blue"  "Yellow"
[4,] "Green" "Red"   
[5,] "Green" "Yellow"
[6,] "Red"   "Yellow"

#You can store it in a data frame for easy access

combinations <- as.data.frame( comboGeneral( names(table(x)) , m=2, freqs = table(x)) )
Posted by: Guest on February-16-2021

Code answers related to "create all unique combinations between elements of a vector R"

Browse Popular Code Answers by Language