[R] give all combinations
Robin Hankin
rksh1 at cam.ac.uk
Mon Sep 1 10:36:04 CEST 2008
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.
>
More information about the R-help
mailing list