Carlos Biagolini
Carlos Biagolini

Reputation: 119

Create a list of combinations and remove pre-used ones

Under R, I have been trying to generate color combinations, and after this I want to import pre-used combinations, remove this for all possible combinations, and finally export the available combinations. I have been trouble to remove these pre-used ones. Thanks for your help! All the best, - Carlos

rm(list=ls());ls()
library("gtools")

# Generate all colors combinations
colors<-c("RED","GREEN","BLUE","BLACK","PINK")
size.of.combinations<-4
all.possible.combinations<-permutations(length(colors),size.of.combinations,colors,repeats.allowed=T)

# Import used data
used.comb<- read.table(file = "used.txt", header = TRUE);   
used.comb<-as.matrix(used.comb)

# Removed pre-used combinations
comb.available<-all.possible.combinations-used.comb #here is the problem

my.data<-data.frame(comb.available)
write.table(my.data, file = "data.csv", sep = ",", col.names = NA, qmethod = "double")

Upvotes: 0

Views: 35

Answers (1)

Sandipan Dey
Sandipan Dey

Reputation: 23101

Remove the pre-used combinations with this, it should work fine (the other lines are okay).

# Removed pre-used combinations
comb.available<- 
             do.call(rbind, strsplit(setdiff(
               apply(all.possible.combinations, 1, function(x) paste(x, collapse=',')), 
               apply(used.comb, 1, function(x) paste(x, collapse=','))), split=','))   #this should work fine

Upvotes: 1

Related Questions