[R] How to do this "combination"?
Liaw, Andy
andy_liaw at merck.com
Thu Sep 9 01:32:54 CEST 2004
> n <- 2
> p <- 5
> m <- do.call("expand.grid", data.frame(matrix(rep(1:n, p), n, p)))
> do.call("paste", c(m, sep=""))
[1] "11111" "21111" "12111" "22111" "11211" "21211" "12211" "22211" "11121"
"21121"
[11] "12121" "22121" "11221" "21221" "12221" "22221" "11112" "21112" "12112"
"22112"
[21] "11212" "21212" "12212" "22212" "11122" "21122" "12122" "22122" "11222"
"21222"
[31] "12222" "22222"
Please give _all_ the details up front, instead of waiting for people to
guess them.
Andy
> From: wanr at ucalgary.ca
>
> 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
> need.
>
> 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.
>
> 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