Raymond Danner
2009-Dec-18 23:27 UTC
[R] Generating permutations that always include one specific element
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]]
Stephan Kolassa
2009-Dec-19 13:38 UTC
[R] Generating permutations that always include one specific element
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.
David Winsemius
2009-Dec-19 13:44 UTC
[R] Generating permutations that always include one specific element
On Dec 18, 2009, at 6:27 PM, Raymond Danner wrote:> 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?Just use the ones that satisfy your requirements: > str(possible.combos) chr [1:73440, 1:4] "AL" "AL" "AL" "AL" "AL" "AL" "AL" "AL" ... > str(apply(possible.combos, 1, function(x) "AL" %in% x)) logi [1:73440] TRUE TRUE TRUE TRUE TRUE TRUE ... > sum(apply(possible.combos, 1, function(x) "AL" %in% x)) [1] 16320 And you will need to clarify what you mean by must be "4 characters long" because none of the strings that would be formed with the method you describe would qualify unless you really mean "4 elements long". -- David Winsemius, MD Heritage Laboratories West Hartford, CT