[R] Fixed size permutations

Henrique Dallazuanna wwwhsd at gmail.com
Thu Jan 14 15:41:20 CET 2010


Try this:

v <- 1:5
n <- 2
t(subset(do.call(expand.grid, replicate(n, v, FALSE))[n:1], Var1 != Var2))

On Thu, Jan 14, 2010 at 9:51 AM, Nick Fankhauser <lists at nyk.ch> wrote:
> I'm using functions to return a matrix of all permutations of a
> specified size from a larger list for predictor selection.
> For each predictor size I use a seperate function like this:
>
> bag2 <- function(n) {
>    rl <- c()
>    for (i1 in seq(n)) {
>        for (i2 in seq(n)) {
>            if (length(unique(c(i1,i2)))==1) {next}
>            rl <- cbind(rl,matrix(c(i1,i2)))
>        }
>    }
>    rl
> }
>
> bag3 <- function(n) {
>    rl <- c()
>    for (i1 in seq(n)) {
>        for (i2 in seq(n)) {
>            for (i3 in seq(n)) {
>                if (length(unique(c(i1,i2,i3)))==1) {next}
>                rl <- cbind(rl,matrix(c(i1,i2,i3)))
>            }
>        }
>    }
>    rl
> }
>
> But I think it should be somehow possible in R to use one general
> function for all sizes. Can someone help?
> I don't exactly know how this kind of permutation is called, maybe this
> would help to find a solution.
>
> Nick
>
> ______________________________________________
> 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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list