[R] How to do this "combination"?

wanr@ucalgary.ca wanr at ucalgary.ca
Thu Sep 9 00:51:30 CEST 2004

Hi all,

source vector = (1,2)
the length of the target vector = 3
My expected "complete" output should be:
111, 112, 121, 122, 211, 212, 221, 222 (they are Not numerical numbers, they 
are equivalent to abc and aab etc.).

Not perfect solutions suggested by others so far: 
solution1: 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.

solution2: as.matrix(expand.grid(1:2, 1:2, 1:2)) almost implements what I 

However, in my case, the length of the target vector is not fixed in 
advance. For example, the output might be 11111, 11112, and etc coming from 
the source vector (1,2).

Thanks in advance.


More information about the R-help mailing list