[R] Simple permutation question

David Reiner David.Reiner at xrtrading.com
Fri Jun 27 18:01:08 CEST 2014


Just for reference, I like package iterpc, because it allows one to get one permutation or combination at a time.
You can get all if you like, but for large N, it is sometimes better to iterate through the results to avoid creating a large object.
Also, getall function returns a matrix so you don't have to mess around with reformatting.
Kudos to Randy Lai and Martin Broadhurst!
David L. Reiner

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Greg Snow
Sent: Thursday, June 26, 2014 1:39 PM
To: Robert Latest
Cc: r-help
Subject: Re: [R] Simple permutation question

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

______________________________________________
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.


This e-mail and any materials attached hereto, including, without limitation, all content hereof and thereof (collectively, "XR Content") are confidential and proprietary to XR Trading, LLC ("XR") and/or its affiliates, and are protected by intellectual property laws.  Without the prior written consent of XR, the XR Content may not (i) be disclosed to any third party or (ii) be reproduced or otherwise used by anyone other than current employees of XR or its affiliates, on behalf of XR or its affiliates.

THE XR CONTENT IS PROVIDED AS IS, WITHOUT REPRESENTATIONS OR WARRANTIES OF ANY KIND.  TO THE MAXIMUM EXTENT PERMISSIBLE UNDER APPLICABLE LAW, XR HEREBY DISCLAIMS ANY AND ALL WARRANTIES, EXPRESS AND IMPLIED, RELATING TO THE XR CONTENT, AND NEITHER XR NOR ANY OF ITS AFFILIATES SHALL IN ANY EVENT BE LIABLE FOR ANY DAMAGES OF ANY NATURE WHATSOEVER, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, CONSEQUENTIAL, SPECIAL AND PUNITIVE DAMAGES, LOSS OF PROFITS AND TRADING LOSSES, RESULTING FROM ANY PERSON'S USE OR RELIANCE UPON, OR INABILITY TO USE, ANY XR CONTENT, EVEN IF XR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR IF SUCH DAMAGES WERE FORESEEABLE.



More information about the R-help mailing list