Skip to content

permutations in R

11 messages · onyourmark, Daniel Viar, Greg Snow +4 more

#
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.
#
Try this:
+ 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
+    }
+ }
[,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:
[,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 <william108 at gmail.com> wrote:
2 days later
#
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,
#
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 <Greg.Snow at imail.org> wrote:
#
On Mar 16, 2009, at 6:43 PM, Stavros Macrakis wrote:

            
<http://search.r-project.org/cgi-bin/namazu.cgi?query=permutations&max=100&result=normal&sort=score&idxname=functions 
 >
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
#
On Mon, 2009-03-16 at 18:43 -0400, Stavros Macrakis wrote:
<snip />
^^^^^^^^^ packages
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
#
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.
#
On Tue, Mar 17, 2009 at 5:24 AM, Gavin Simpson <gavin.simpson at ucl.ac.uk> wrote:
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
#
On Tue, Mar 17, 2009 at 12:41 PM, Greg Snow <Greg.Snow at imail.org> wrote:
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
#
Stavros Macrakis wrote:
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.
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.
"
*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
1 day later
#
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,