2016-12-14 90 views
0

在R下,我一直在尝试生成颜色组合,之后我想导入预先使用的组合,请删除所有可能的组合,最后导出可用组合组合。我一直很难删除这些预先使用的。谢谢你的帮助! 一切顺利, - 卡洛斯创建组合列表并删除预先使用的组合

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") 

回答

1

删除与此预组合使用,它应该工作正常(其他线路都还好)。

# 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 
+0

非常感谢!它完美的工作! –