From: Daniel Viar <dan.viar_at_gmail.com>

Date: Fri, 13 Mar 2009 22:33:18 -0500

>

> fn_perm_list(3,3)

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

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

>

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. Received on Sat 14 Mar 2009 - 02:38:27 GMT

Date: Fri, 13 Mar 2009 22:33:18 -0500

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 <william108_at_gmail.com> wrote:

>

> Hi. Does anyone know of a function which will take as input a number n (or a

>

> For example if n is 3, I would want a function which I could use in a loop

>

> Thank you.

>

> ______________________________________________

>

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. Received on Sat 14 Mar 2009 - 02:38:27 GMT

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Sat 14 Mar 2009 - 03:30:26 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*