# [R] Simple permutation question

David L Carlson dcarlson at tamu.edu
Wed Jun 25 23:07:34 CEST 2014

```A couple of typos in the last section. It should be

> perm.ltrs <- apply(indx, 1, function(x) paste(LETTERS[x], collapse=" "))
> tail(perm.ltrs)
[1] "E D A B C" "E D B A C" "E D B C A" "E D A C B" "E D C A B" "E D C B A"
> tail(perm.ltrs)
[1] "E D A B C" "E D B A C" "E D B C A" "E D A C B" "E D C A B" "E D C B A"
>

David C

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of David L Carlson
Sent: Wednesday, June 25, 2014 4:02 PM
Cc: r-help at r-project.org
Subject: Re: [R] Simple permutation question

Assuming you want all of the permutations not just a random permutation (such as sample() give you):

> require(e1071)
> indx <- permutations(5)
[,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    2    1    3    4    5
[3,]    2    3    1    4    5
[4,]    1    3    2    4    5
[5,]    3    1    2    4    5
[6,]    3    2    1    4    5
> tail(indx)
[,1] [,2] [,3] [,4] [,5]
[115,]    5    4    1    2    3
[116,]    5    4    2    1    3
[117,]    5    4    2    3    1
[118,]    5    4    1    3    2
[119,]    5    4    3    1    2
[120,]    5    4    3    2    1

If you want them converted to numbers try
> apply(ind, 1, function(x) paste(LETTERS[x], collapse=" "))
[1] "A B C D E" "B A C D E" "B C A D E" "A C B D E" "C A B D E" "C B A D E"
> tail(perm.ltrs)
[1] "E D A B C" "E D B A C" "E D B C A" "E D A C B" "E D C A B" "E D C B A"

This is not the only permutation function in R, but this one has the advantage of being symmetrical. The last permutation is the reverse of the first, the penultimate the reverse of the second, etc.

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Cade, Brian
Sent: Wednesday, June 25, 2014 3:39 PM
To: Robert Latest
Cc: r-help at r-project.org
Subject: Re: [R] Simple permutation question

It is called sample(,replace=F), where the default argument is sampling
without replacement.
Try
x <- c("A","B","C","D","E")
sample(x)

Brian

U. S. Geological Survey
Fort Collins Science Center
2150 Centre Ave., Bldg. C
Fort Collins, CO  80526-8818

tel:  970 226-9326

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
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help