# [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[])
}
if (length(args) > 2) {
jj <- do.call("Recall", c(args[-1]))
return(do.call("Recall", c(list(args[]), list(jj) )))
}
a <- args[]
b <- args[]
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].

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