[R] How to do this "combination" ?

Erich Neuwirth erich.neuwirth at univie.ac.at
Thu Sep 9 01:26:22 CEST 2004


combinat<-function(els,len){
if (len==1) (return(as.character(els)))
else return(as.vector(t(outer(combinat(els,len-1),as.character(els),
     function(x,y)paste(x,y,sep="")))))
}

combinat(1:2,3)
does what you want



wanr at ucalgary.ca wrote:

> Thanks David first.
> 
> Probably, David misunderstood my points.
> 
> source vector = (1,2)
> the length of the target vector = 3
> 
> My expected "complete" outputs should be:
> 111, 112, 121, 122, 211, 212, 221, 222.
> 
> sample(c(1,2),3,replace=TRUE) just gives me one "randomly sampled" 
> combination at a time. However, I need the whole list displayed above at a 
> time.
> 
> Rui
> 
> ______________________________________________
> 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
> 


-- 
Erich Neuwirth, Computer Supported Didactics Working Group
Visit our SunSITE at http://sunsite.univie.ac.at
Phone: +43-1-4277-38624 Fax: +43-1-4277-9386




More information about the R-help mailing list