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. |
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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
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. |
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? <http://search.r-project.org/cgi-bin/namazu.cgi?query=permutations&max=100&result=normal&sort=score&idxname=functions > > > > 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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
In reply to this post by macrakis
On Mon, 2009-03-16 at 18:43 -0400, Stavros Macrakis wrote:
> Greg, <snip /> > > 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 ^^^^^^^^^ packages > 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? That is what the Task Views are meant to address, for discrete subject areas. Of course, if this particular topic isn't covered then that means no one has yet been stimulated enough to produce one; the task views are all user efforts supported by Achim Zeileis and Kurt Hornik's work on the ctv package providing the R interface to these resources on CRAN. http://cran.at.r-project.org/web/views/ G > > 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. Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% ______________________________________________ [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. signature.asc (204 bytes) Download Attachment |
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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
In reply to this post by Gavin Simpson
On Tue, Mar 17, 2009 at 5:24 AM, Gavin Simpson <[hidden email]> wrote:
> On Mon, 2009-03-16 at 18:43 -0400, Stavros Macrakis wrote: >> ... no way to find relevant functions, and no way of knowing which one to use if there is more than one. > That is what the Task Views are meant to address, for discrete subject areas. Thanks for the information about Task Views -- I was completely unaware of this effort, which looks very valuable. Shouldn't it be linked from somewhere in the 'documentation' section of r-project.org? Since it was not, I was unaware of the master CRAN documentation page (http://cran.r-project.org/) -- and I still don't know how to get to this page from the R-project home page. The only link to CRAN info on the R-project home page is entitled "Download"; when you click on CRAN, you get a list of mirror sites. If you then click on any particular mirror site, you get a page (e.g. http://lib.stat.cmu.edu/R/CRAN/) which talks about downloading binaries and source. On clicking Software/Packages, you get to a page with an alphabetical list of a large number of packages, which finally mentions CRAN Task Views. It's a pity that such valuable information is so hard to find in the site. It would be useful to add better "information scent" to the various links, and also to flatten the hierarchy a bit. Thanks again for the pointer. -s ______________________________________________ [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. |
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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
In reply to this post by macrakis
Stavros Macrakis wrote:
> On Tue, Mar 17, 2009 at 5:24 AM, Gavin Simpson <[hidden email]> wrote: > >> On Mon, 2009-03-16 at 18:43 -0400, Stavros Macrakis wrote: >> >>> ... no way to find relevant functions, and no way of knowing which one to use if there is more than one. >>> > > >> That is what the Task Views are meant to address, for discrete subject areas. >> > > Thanks for the information about Task Views -- I was completely > unaware of this effort, which looks very valuable. > > Shouldn't it be linked from somewhere in the 'documentation' section > of r-project.org? > > Since it was not, I was unaware of the master CRAN documentation page > (http://cran.r-project.org/) -- and I still don't know how to get to > this page from the R-project home page. The only link to CRAN info on > the R-project home page is entitled "Download"; not exactly true; the links 'manuals' and 'faqs' under 'documentation' on the r homepage will lead you to cran, too. but it's a pity that the r homepage is not explicit about what 'cran' means, and does not link to the root of cran. but if you type 'cran' in the address field of your browser (at least in firefox, though not in opera), you should get the cran homepage. but you need to know in advance. > when you click on > CRAN, you get a list of mirror sites. If you then click on any > particular mirror site, you get a page (e.g. > http://lib.stat.cmu.edu/R/CRAN/) which talks about downloading > binaries and source. On clicking Software/Packages, you get to a page > with an alphabetical list of a large number of packages, which finally > mentions CRAN Task Views. wow! the only way i knew to get to the task views was to search the mailing lists archives for a post where the task views were mentioned, and then follow the link. seriously, once you're at the cran homepage, you have a direct and explicit link to task views in the 'cran' section in the menu. also, when you choose 'packages' under 'software' in the menu, you see a page where it says " CRAN Task Views allow you to browse packages by topic and provide tools to automatically install all packages for special areas of interest. Currently, 23 views are available. " > It's a pity that such valuable information > is so hard to find in the site. *so* hard? on the side, the task views are curated manually, with all benefits and drawbacks of this approach. it could be helpful if packages were indexed, e.g., by keywords, tags, or a more structured vocabulary, allowing a user to construct 'task views' dynamically, depending on the values of a selection of criteria. there is the rsitesearch [1], but it will only "allow you to search the contents of most of the r functions, and r-help mail archives", which is not quite the same as browsing a package repository organized with a collection of, possibly structured, keywords. vQ ______________________________________________ [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. |
In reply to this post by macrakis
Yes, it would have been better if I had mentioned in my original reply that it was an omegahat package. I found it by using help.search() (or ??) where that package was installed on the computer where I did the search. Looking at the help for setRepositories and packageStatus show one way to see what packages are available from bioconductor and omegahat.
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:[hidden email]] On Behalf Of > Stavros Macrakis > Sent: Tuesday, March 17, 2009 10:46 PM > To: Greg Snow > Cc: onyourmark; [hidden email] > Subject: Re: [R] permutations in R > > 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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Free forum by Nabble | Edit this page |