[R] r: LEXICOGRAPHIC ORDERING

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Fri May 27 15:47:06 CEST 2005


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


----- Original Message ----- 
From: "Clark Allan" <Allan at stats.uct.ac.za>
To: <r-help at stat.math.ethz.ch>
Sent: Friday, May 27, 2005 3:24 PM
Subject: [R] r: LEXICOGRAPHIC ORDERING


> 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 at 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




More information about the R-help mailing list