Re: [R] r: LEXICOGRAPHIC ORDERING

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Fri 27 May 2005 - 23:47:06 EST

here is a possible solution:

subsets <- function (n, r, s = 1:n) {

    # from V&Rs (2000) S Programming, Springer, p.49

    if (mode(n) != "numeric" || length(n) != 1 || n < 1 || (n%%1) != 
0) stop("bad value of n")
    if (mode(r) != "numeric" || length(r) != 1 || r < 1 || (r%%1) != 
0) stop("bad value of r")
    if (!is.atomic(s) || length(s) < n) stop("s is either non-atomic 
or too short")

    fun <- function(n, r, s)

        if (r <= 0) vector(mode(s), 0)
        else if (r >= n) s[1:n]
        else rbind(cbind(s[1], Recall(n - 1, r - 1, s[-1])), 
Recall(n - 1, r, s[-1]))

    fun(n, r, s)
}
##############
x <- c(1, 3, 5, 7, 9)

ind <- subsets(length(x), 2)
apply(matrix(x[ind], nc = ncol(ind)), 1, function(x) as.numeric(paste(x, collapse = "")))

ind <- subsets(length(x), 3)
apply(matrix(x[ind], nc = ncol(ind)), 1, function(x) as.numeric(paste(x, collapse = "")))

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


> HI all
>
> i have a seemingly simple question.
>
> given a sequence of numbers say, 1,2,3,4,5.
>
> i would like to get all of the possible two number arrangments
> (combinations), all 3 number arrangents ... 5 number arrangements
> (combinations).
>
> i.e.
>
> in the 2 number case:
>
> 12,13,14,15,23,24,25,34,35,45
>
>
>
> any ideas?


> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri May 27 23:59:38 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:11 EST