[R] Generating permutations that always include one specific element

Stephan Kolassa Stephan.Kolassa at gmx.de
Sat Dec 19 14:38:36 CET 2009


Hi Ray,

First possibility: just select those combinations that contain "AL":
combos.with.AL <- possible.combos[rowSums(possible.combos=="AL")>0,]

Second possibility: create all 3-combos *without* "AL":
bands.without.AL <- c("B", "DB", "DG", "G", "K", "LB", "LG", "MG", "O", 
"P", "PI", "PK", "PU", "R", "V", "W", "Y")
possible.combos.without.AL <- permutations(17, 3, bands.without.AL)

Then insert "AL" in the first, second, third and fourth position of this 
matrix:
foo <- cbind("AL",possible.combos.without.AL)
combos.with.AL <- 
rbind(foo,foo[,c(2,1,3,4)],foo[,c(2,3,1,4)],foo[,c(2,3,4,1)])

The first one is easier to understand but requires you to first build 
the big object possible combos, most of which you discard. May be a 
problem in larger instances.

HTH,
Stephan


Raymond Danner schrieb:
> Dear R community,
> 
> I am trying to create a matrix of permutations of a vector:
> bands <- c("AL", "B", "DB", "DG", "G", "K", "LB", "LG", "MG", "O", "P",
> "PI", "PK", "PU", "R", "V", "W", "Y")
> 
> Each permutation must be 4 characters long. permutations() from the gtools
> package does this easy enough:
> possible.combos <- permutations(18, 4, bands)
> 
> However, ³AL² must be one of the elements in each permutation.
> Any ideas?
> 
> Thanks in advance,
> Ray 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list