2

How do I create a matrix in R with 50 individuals having labels K=1,2,3 and then all possible outcomes? It does not seem to be either combinations or per muations. So as an example with N=2 the following, only then not 2 rows but 50. So I get a 50x3^50 matrix.

> cbind(c(1,1),c(1,2),c(1,3),c(2,1),c(2,2),c(2,3),c(3,1),c(3,2),c(3,3))
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    1    1    2    2    2    3    3    3
[2,]    1    2    3    1    2    3    1    2    3

And for N=3:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27]
[1,]    1    1    1    1    1    1    1    1    1     2     2     2     2     2     2     2     2     2     3     3     3     3     3     3     3     3     3
[2,]    1    1    1    2    2    2    3    3    3     1     1     1     2     2     2     3     3     3     1     1     1     2     2     2     3     3     3
[3,]    1    2    3    1    2    3    1    2    3     1     2     3     1     2     3     1     2     3     1     2     3     1     2     3     1     2     3
user773674
  • 33
  • 3
  • I think you are talking about *permutations*. See https://stackoverflow.com/questions/11095992/generating-all-distinct-permutations-of-a-list-in-r, and perhaps see `gtools::permutations`. – r2evans Jun 22 '22 at 22:15
  • I agree it looks like a permutation but its not. – user773674 Jun 22 '22 at 22:21
  • Okay, thanks. It does not appear to be *combinations*, though, so I think the question would benefit from clarification. – r2evans Jun 22 '22 at 22:26
  • 3
    You can't get all possible combinations. Look at your example above. When N = 2 you have 3^2 combinations. When N = 3 you have 3^3 combinations. In general you have 3^N combinations. When N = 50, you have 3^50 or 7 * 10^23. There is literally not enough computer storage capacity in the world to hold this amount of data. – Allan Cameron Jun 22 '22 at 22:30
  • Would a sample of the permutations be useful? If so sample() with replace=FALSE might be useful – John Garland Jun 22 '22 at 23:05
  • These are also not permutations. I **think** that `replicate(N, sample(1:K))` will give a uniform samples from the columns. – Ben Bolker Jun 22 '22 at 23:11

2 Answers2

2

This gets you partway there but I think there might be a practical problem with your definition. What it sounds like you want is impractical for N=50.

f <- function(n, K) {
    (replicate(n, 1:K, simplify = FALSE)  ## n copies of 1:K
        |> do.call(what = expand.grid)    ## pass these all to expand.grid
        |> as.matrix()                    ## convert to matrix
        |> t()                            ## transpose
    )
}

f(2) and f(3) look like your examples above. However ...

  • f(2) is 2 x 9 (i.e., 2 by K^2)
  • f(3) is 3 x 27 (3 by K^3)
  • f(4) is 4 x 81 (3 by K^4)

... so if we continue on this way, the results for N=50 will not be 50 x 50^2 but N x K^N = 50 x 3^50, which is 3.5 x 10^25 ...

... so you might need to rethink something about your strategy. (50^3 would be OK, but 3^50 is not!)

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • haha it was a typo, But indeed no way this gets stored, time to rethink I guess... – user773674 Jun 22 '22 at 22:33
  • If this solves your *technical* problem (it doesn't sound like it solves your ultimate problem) you're encouraged to click on the check-mark to accept it. (If you only needed to go up to, say, N=15, this would work ...) – Ben Bolker Jun 22 '22 at 22:36
0

You can try the code below for code-golfing if you like

k <- 3
n <- 4
t(expand.grid(rep(list(1:k), n)))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81