Re: [R] give all combinations

From: Robin Hankin <rksh1_at_cam.ac.uk>
Date: Mon, 01 Sep 2008 09:36:04 +0100

Hi Yuan, Lucien, List.

try this:

f <-
function (...)
{

    args <- list(...)
    if(length(args)==0){

        return(NULL)
    }
    if (length(args) == 1) {

        return(args[[1]])
    }
    if (length(args) > 2) {

        jj <- do.call("Recall", c(args[-1]))
        return(do.call("Recall", c(list(args[[1]]), list(jj) )))
    }
    a <- args[[1]]
    b <- args[[2]]
    if (is.null(b)) {

        return(a)
    }
    jj <- outer(a,b,paste)
    return(jj[!lower.tri(jj)])
}

[the difficult bit (IMO) is to make f() work with any number of arguments; thus
f(a,a) and f(a,a,a,a,a,b,b,a,b) and whatever should also work and this is why
the Recall bit is needed].

Comments anyone?

HTH rksh

Lucien Lemmens wrote:
>
>
> Another solution requiring also a bit of programming is:
>
> l<-letters[1:3]
> c2<-c()
> for(i in 1:3){c2<-c(c2,paste(letters[i],letters[i:3],sep=""))}
> c2
> [1] "aa" "ab" "ac" "bb" "bc" "cc"
> n<-length(c2)
> c3<-c();for(i in 1:n){c3<-c(c3,paste(c2[i],letters[ceiling(i/2):3],sep=""))}
> c3
> [1] "aaa" "aab" "aac" "aba" "abb" "abc" "acb" "acc" "bbb" "bbc" "bcc" "ccc"
>
> ______________________________________________
> 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 Mon 01 Sep 2008 - 08:38:29 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 Mon 01 Sep 2008 - 09:34:08 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.

list of date sections of archive