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
Try:> m <- as.matrix(expand.grid(1:2, 1:2, 1:2)) > x <- m[,1] * 100 + m[,2] * 10 + m[,3] > x1 2 3 4 5 6 7 8 111 211 121 221 112 212 122 222 Note that if the `source vector' is not 1:k for some integer k, you need to coerce the output of expand.grid, as that returns a data frame of factors. Andy> From: wanr at ucalgary.ca > > 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 > >
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