[R] Simple permutation question
Greg Snow
538280 at gmail.com
Thu Jun 26 20:39:27 CEST 2014
For your 2nd question (which also answers your first question) I use
the permn function in the combinat package, this function is nice that
in addition to generating all the permutations it will also,
optionally, run a function on each permutation for you:
> t(simplify2array( permn( c("A","B","C") ) ))
[,1] [,2] [,3]
[1,] "A" "B" "C"
[2,] "A" "C" "B"
[3,] "C" "A" "B"
[4,] "C" "B" "A"
[5,] "B" "C" "A"
[6,] "B" "A" "C"
> unlist( permn( c("A","B","C"), fun=paste, collapse='' ) )
[1] "ABC" "ACB" "CAB" "CBA" "BCA" "BAC"
In addition, running help.search on my computer (RSiteSearch, or the
SOS package may return even more) shows the following functions as
generating permutations:
combinat::permn
e1071::permutations
gtools::permutations
multicool::allPerm
pracma::perms
prob::permsn
sna::numperm
On Wed, Jun 25, 2014 at 2:22 PM, Robert Latest <boblatest at gmail.com> wrote:
> So my company has hired a few young McKinsey guys from overseas for a
> couple of weeks to help us with a production line optimization. They
> probably charge what I make in a year, but that's OK because I just
> never have the time to really dive into one particular time, and I have
> to hand it to the consultants that they came up with one or two really
> clever ideas to model the production line. Of course it's up to me to
> feed them the real data which they then churn through their Excel
> models that they cook up during the nights in their hotel rooms, and
> which I then implement back into my experimental system using live data.
>
> Anyway, whenever they need something or come up with something I skip
> out of the room, hack it into R, export the CSV and come back in about
> half the time it takes Excel to even read in the data, let alone
> process it. Of course that gor them curious, and I showed off a couple
> of scripts that condense their abysmal Excel convolutions in a few
> lean and mean lines of R code.
>
> Anyway, I'm in my office with this really attractive, clever young
> McKinsey girl (I'm in my mid-forties, married with kids and all, but I
> still enjoyed impressing a woman with computer stuff, of all things!),
> and one of her models involves a simple permutation of five letters --
> "A" through "E".
>
> And that's when I find out that R doesn't have a permutation function.
> How is that possible? R has EVERYTHING, but not that? I'm
> flabbergasted. Stumped. And now it's up to me to spend the evening at
> home coding that model, and the only thing I really need is that
> permutation.
>
> So this is my first attempt:
>
> perm.broken <- function(x) {
> if (length(x) == 1) return(x)
> sapply(1:length(x), function(i) {
> cbind(x[i], perm(x[-i]))
> })
> }
>
> But it doesn't work:
>> perm.broken(c("A", "B", "C"))
> [,1] [,2] [,3]
> [1,] "A" "B" "C"
> [2,] "A" "B" "C"
> [3,] "B" "A" "A"
> [4,] "C" "C" "B"
> [5,] "C" "C" "B"
> [6,] "B" "A" "A"
>>
>
> And I can't figure out for the life of me why. It should work because I
> go through the elements of x in order, use that in the leftmost column,
> and slap the permutation of the remaining elements to the right. What
> strikes me as particularly odd is that there doesn't even seem to be a
> systematic sequence of letters in any of the columns. OK, since I
> really need that function I wrote this piece of crap:
>
> perm.stupid <- function(x) {
> b <- as.matrix(expand.grid(rep(list(x), length(x))))
> b[!sapply(1:nrow(b), function(r) any(duplicated(b[r,]))),]
> }
>
> It works, but words cannot describe its ugliness. And it gets really
> slow really fast with growing x.
>
> So, anyway. My two questions are:
> 1. Does R really, really, seriously lack a permutation function?
> 2. OK, stop kidding me. So what's it called?
> 3. Why doesn't my recursive function work, and what would a
> working version look like?
>
> Thanks,
> robert
>
> ______________________________________________
> 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.
--
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com
More information about the R-help
mailing list