[R] Simple permutation question

Robert Latest boblatest at gmail.com
Wed Jun 25 22:22:12 CEST 2014


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



More information about the R-help mailing list