# permutations in R

11 messages
Open this post in threaded view
|
Report Content as Inappropriate

## permutations in R

 Hi. Does anyone know of a function which will take as input a number n (or a set of n letters) and will give out, one at a time, the permutations of n (or of those n letters) as a vector? So that I can use the permutations one at a time. And such that it will exhaust all the permutations with no repeats. For example if n is 3, I would want a function which I could use in a loop and the first time I use it in the loop it may give the vector 123 and then the next time in the loop it may give 132 and so on until after 6 iterations through the loop I would get all 6 permutations of 123. Thank you.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: permutations in R

 Try this: > # Taken from combinations(gtools) > # library(gregmisc) > # Function permutations > fn_perm_list <- + function (n, r, v = 1:n) + { +    if (r == 1) +       matrix(v, n, 1) +    else if (n == 1) +       matrix(v, 1, r) +    else { +       X <- NULL +       for (i in 1:n) X <- rbind(X, cbind(v[i], fn_perm_list(n - +            1, r - 1, v[-i]))) +        X +    } + } > > fn_perm_list(3,3)      [,1] [,2] [,3] [1,]    1    2    3 [2,]    1    3    2 [3,]    2    1    3 [4,]    2    3    1 [5,]    3    1    2 [6,]    3    2    1 Note that the you can use library gregmisc without using this function, but I thought it might be instructive for you to see how this is done. Here's how you would normally do this: > library(gregmisc) > permutations(3,3)      [,1] [,2] [,3] [1,]    1    2    3 [2,]    1    3    2 [3,]    2    1    3 [4,]    2    3    1 [5,]    3    1    2 [6,]    3    2    1 Cheers, Dan Viar On Fri, Mar 13, 2009 at 8:06 PM, onyourmark <[hidden email]> wrote: > > Hi. Does anyone know of a function which will take as input a number n (or a > let of n letters) and will give out, one at a time, the permutations of n > (or of those n letters) as a vector? > So that I can use the permutations one at a time. And such that it will > exhaust all the permutations with no repeats. > > For example if n is 3, I would want a function which I could use in a loop > and the first time I use it in the loop it may give the vector > 123 > and then the next time in the loop it may give > 132 > and so on so that after 6 iterations through the loop I would get all 6 > permutations of 123. > > Thank you. > -- > View this message in context: http://www.nabble.com/permutations-in-R-tp22507989p22507989.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: permutations in R

 In reply to this post by onyourmark2012 Look at the permutations function in the Combinations package.  Using the "fun" argument may accomplish what you want.  If not, there are references on the help page to other code that may work for you. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [hidden email] 801.408.8111 > -----Original Message----- > From: [hidden email] [mailto:r-help-bounces@r- > project.org] On Behalf Of onyourmark > Sent: Friday, March 13, 2009 7:09 PM > To: [hidden email] > Subject: [R] permutations in R > > > Hi. Does anyone know of a function which will take as input a number n > (or a > set of n letters) and will give out, one at a time, the permutations of > n > (or of those n letters) as a vector? > So that I can use the permutations one at a time. And such that it will > exhaust all the permutations with no repeats. > > For example if n is 3, I would want a function which I could use in a > loop > and the first time I use it in the loop it may give the vector > 123 > and then the next time in the loop it may give > 132 > and so on until after 6 iterations through the loop I would get all 6 > permutations of 123. > > Thank you. > -- > View this message in context: http://www.nabble.com/permutations-in-R-> tp22507989p22507989.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: permutations in R

 Greg, Thanks for helping this user. I assume you mean the permn function in the combinat package? For a new user (including me), it is not obvious how to get from "the permutations function in the Combinations package" to that. I see there is also a function gtools::permutations.  The gtools package seems to be more recent than combinat, but I don't know if it is better.  You might expect a utils::permn since there is a utils::combn, but there isn't one.... I'm not bringing this up because I am bloody-minded, but because I think it's a serious issue with the R libraries -- there is no obvious way to find relevant functions, and no way of knowing which one to use if there is more than one.  Or am I missing some trick? Thanks,               -s On Mon, Mar 16, 2009 at 3:26 PM, Greg Snow <[hidden email]> wrote: > Look at the permutations function in the Combinations package.  Using the "fun" argument may accomplish what you want.  If not, there are references on the help page to other code that may work for you. > > Hope this helps, > > -- > Gregory (Greg) L. Snow Ph.D. > Statistical Data Center > Intermountain Healthcare > [hidden email] > 801.408.8111 > > >> -----Original Message----- >> From: [hidden email] [mailto:r-help-bounces@r- >> project.org] On Behalf Of onyourmark >> Sent: Friday, March 13, 2009 7:09 PM >> To: [hidden email] >> Subject: [R] permutations in R >> >> >> Hi. Does anyone know of a function which will take as input a number n >> (or a >> set of n letters) and will give out, one at a time, the permutations of >> n >> (or of those n letters) as a vector? >> So that I can use the permutations one at a time. And such that it will >> exhaust all the permutations with no repeats. >> >> For example if n is 3, I would want a function which I could use in a >> loop >> and the first time I use it in the loop it may give the vector >> 123 >> and then the next time in the loop it may give >> 132 >> and so on until after 6 iterations through the loop I would get all 6 >> permutations of 123. >> >> Thank you. >> -- >> View this message in context: http://www.nabble.com/permutations-in-R->> tp22507989p22507989.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> [hidden email] 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. > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: permutations in R

 On Mar 16, 2009, at 6:43 PM, Stavros Macrakis wrote: > Greg, > > Thanks for helping this user. > > I assume you mean the permn function in the combinat package? For a > new user (including me), it is not obvious how to get from "the > permutations function in the Combinations package" to that. > > I see there is also a function gtools::permutations.  The gtools > package seems to be more recent than combinat, but I don't know if it > is better.  You might expect a utils::permn since there is a > utils::combn, but there isn't one.... > > I'm not bringing this up because I am bloody-minded, but because I > think it's a serious issue with the R libraries -- there is no obvious > way to find relevant functions, and no way of knowing which one to use > if there is more than one.  Or am I missing some trick? > > > Thanks, > >              -s > > > On Mon, Mar 16, 2009 at 3:26 PM, Greg Snow <[hidden email]>   > wrote: >> Look at the permutations function in the Combinations package.   >> Using the "fun" argument may accomplish what you want.  If not,   >> there are references on the help page to other code that may work   >> for you. >> >> Hope this helps, >> >> -- >> Gregory (Greg) L. Snow Ph.D. >> Statistical Data Center >> Intermountain Healthcare >> [hidden email] >> 801.408.8111 >> >> >>> -----Original Message----- >>> From: [hidden email] [mailto:r-help-bounces@r- >>> project.org] On Behalf Of onyourmark >>> Sent: Friday, March 13, 2009 7:09 PM >>> To: [hidden email] >>> Subject: [R] permutations in R >>> >>> >>> Hi. Does anyone know of a function which will take as input a   >>> number n >>> (or a >>> set of n letters) and will give out, one at a time, the   >>> permutations of >>> n >>> (or of those n letters) as a vector? >>> So that I can use the permutations one at a time. And such that it   >>> will >>> exhaust all the permutations with no repeats. >>> >>> For example if n is 3, I would want a function which I could use   >>> in a >>> loop >>> and the first time I use it in the loop it may give the vector >>> 123 >>> and then the next time in the loop it may give >>> 132 >>> and so on until after 6 iterations through the loop I would get   >>> all 6 >>> permutations of 123. >>> >>> Thank you. >>> -- >>> View this message in context: http://www.nabble.com/permutations-in-R->>> tp22507989p22507989.html >>> Sent from the R help mailing list archive at Nabble.com. >>> >>> ______________________________________________ >>> [hidden email] 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. >> >> ______________________________________________ >> [hidden email] 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. >> > > ______________________________________________ > [hidden email] 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, MD Heritage Laboratories West Hartford, CT ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: permutations in R

Open this post in threaded view
|
Report Content as Inappropriate

## Re: permutations in R

 In reply to this post by macrakis No, I meant the Combinations package, it is apparently an Omegahat package (http://www.omegahat.org/Combinations/).  It looks similar to the permn function as far as the usage goes, but the documentation includes additional information on other resources. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [hidden email] 801.408.8111 > -----Original Message----- > From: [hidden email] [mailto:[hidden email]] On Behalf Of > Stavros Macrakis > Sent: Monday, March 16, 2009 4:43 PM > To: Greg Snow > Cc: onyourmark; [hidden email] > Subject: Re: [R] permutations in R > > Greg, > > Thanks for helping this user. > > I assume you mean the permn function in the combinat package? For a > new user (including me), it is not obvious how to get from "the > permutations function in the Combinations package" to that. > > I see there is also a function gtools::permutations.  The gtools > package seems to be more recent than combinat, but I don't know if it > is better.  You might expect a utils::permn since there is a > utils::combn, but there isn't one.... > > I'm not bringing this up because I am bloody-minded, but because I > think it's a serious issue with the R libraries -- there is no obvious > way to find relevant functions, and no way of knowing which one to use > if there is more than one.  Or am I missing some trick? > > Thanks, > >               -s > > > On Mon, Mar 16, 2009 at 3:26 PM, Greg Snow <[hidden email]> wrote: > > Look at the permutations function in the Combinations package.  Using > the "fun" argument may accomplish what you want.  If not, there are > references on the help page to other code that may work for you. > > > > Hope this helps, > > > > -- > > Gregory (Greg) L. Snow Ph.D. > > Statistical Data Center > > Intermountain Healthcare > > [hidden email] > > 801.408.8111 > > > > > >> -----Original Message----- > >> From: [hidden email] [mailto:r-help-bounces@r- > >> project.org] On Behalf Of onyourmark > >> Sent: Friday, March 13, 2009 7:09 PM > >> To: [hidden email] > >> Subject: [R] permutations in R > >> > >> > >> Hi. Does anyone know of a function which will take as input a number > n > >> (or a > >> set of n letters) and will give out, one at a time, the permutations > of > >> n > >> (or of those n letters) as a vector? > >> So that I can use the permutations one at a time. And such that it > will > >> exhaust all the permutations with no repeats. > >> > >> For example if n is 3, I would want a function which I could use in > a > >> loop > >> and the first time I use it in the loop it may give the vector > >> 123 > >> and then the next time in the loop it may give > >> 132 > >> and so on until after 6 iterations through the loop I would get all > 6 > >> permutations of 123. > >> > >> Thank you. > >> -- > >> View this message in context: http://www.nabble.com/permutations-in-> R- > >> tp22507989p22507989.html > >> Sent from the R help mailing list archive at Nabble.com. > >> > >> ______________________________________________ > >> [hidden email] 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. > > > > ______________________________________________ > > [hidden email] 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. > > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: permutations in R

Open this post in threaded view
|
Report Content as Inappropriate

## Re: permutations in R

 In reply to this post by Greg Snow-2 On Tue, Mar 17, 2009 at 12:41 PM, Greg Snow <[hidden email]> wrote: > No, I meant the Combinations package, it is apparently an Omegahat package (http://www.omegahat.org/Combinations/).  It looks similar to the permn function as far as the usage goes, but the documentation includes additional information on other resources. Argh!  For a new user (including me), it is even less obvious how to get from "the permutations function in the Combinations package" to omegahat.org! Rseek search does find it as result #1, but the search box in http://www.r-project.org/index.html does not use Google Custom Search (as does Rseek), but Google Site Search. Argh!  If I had any hair left, I'd tear it out!            -s ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate