[R] How to do this "combination" ?

Liaw, Andy andy_liaw at merck.com
Thu Sep 9 00:18:36 CEST 2004


Try:

> m <- as.matrix(expand.grid(1:2, 1:2, 1:2))
> x <- m[,1] * 100 + m[,2] * 10 + m[,3]
> x
  1   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
> 
>




More information about the R-help mailing list