How to assign a set of numbers randomly to a matrix in R

柔情痞子 提交于 2020-01-15 09:30:15

问题


I need to assign fixed numbers, let's say "x", "y", "z", etc., randomly to a matrix. How can I do that? I did search about it but they all explained how to make a matrix with random numbers. But my numbers aren't random. I know what numbers I want in my matrix, I don't know how to assign them randomly to my matrix. And I can't write any line of codes for that to put it here to correct it.

Here is an example. Assume that I have numbers 1 and 1.2. I want to generate a 20*10 matrix with its elements randomly chosen from 1 and 1.2 . An example matrix looks like this:

1    1     1.2  1
1.2  1     1.2  1.2
1    1.2   1    1.2
1    1.2   1    1

Each number should occur in each row at least once.

Thanks a lot


回答1:


Solution

t(replicate(20, sample(c(1, 1.2, sample(c(1, 1.2), 8, replace = TRUE)))))

Explanation

sample(c(1, 1.2, sample(c(1, 1.2), 8, replace = TRUE)))

creates a randomly ordered sample of 10 that ensures one of the elements will be 1, one of the elements will be 1.2, and the other 8 will be randomly selected from 1 and 1.2.

t(replicate(20, sample(c(1, 1.2, sample(c(1, 1.2), 8, replace = TRUE)))))

does this 20 times and transposes the answer to be of the dimensions you'd like.

UPDATE

After additional comments, it seems you need to be able to accomplish two different things:

  1. Create an n by m matrix randomly filled with the values of x subject to the constraint that each row of the resulting matrix contains every element of x at least once.
  2. Create an n by m matrix randomly filled with the values of x subject to the constraint that each row of the resulting matrix has more than one unique value.

So, at this point, I would recommend creating functions:

f1 <- function(x, n, m) {
    N <- length(x)
    if ( N > m ) {
        stop('x is longer than the number of columns requested.', call. = FALSE)
    }
    return(t(replicate(n, sample(c(x, sample(x, m - N, replace = TRUE))))))
}

f2 <- function(x, n, m) {
    if ( length(unique(x)) == 1 ) {
        stop('x has only one unique element.', call. = FALSE)
    }
    result <- t(replicate(n, sample(x, m, replace = TRUE)))
    while ( any(apply(result, 1, function(x) length(unique(result)) == 1)) ) {
        result <- t(replicate(n, sample(x, m, replace = TRUE)))
    }
    return(result)
}

(If I were you I'd also give those functions more informative names).

f1() accomplishes what my original answer does (corresponding to number 1. above) for arbitrary x, n, and m. f2() accomplishes the new request (corresponding to number 2. above); however, note there are probably better ways to accomplish this task, and this approach (a while loop) could take an arbitrary amount of time depending on the values of x, n, m, and chance. Here are example calls of the functions:

set.seed(1234)
x <- c(1, 1.2)
f1(x, 20, 10)

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]  1.2  1.2  1.2  1.2  1.2  1.0  1.2  1.0  1.0   1.0
 [2,]  1.0  1.0  1.0  1.0  1.0  1.2  1.0  1.0  1.0   1.2
 [3,]  1.0  1.2  1.2  1.2  1.2  1.2  1.0  1.0  1.2   1.0
 [4,]  1.2  1.0  1.2  1.0  1.2  1.0  1.2  1.2  1.0   1.0
 [5,]  1.2  1.0  1.2  1.2  1.0  1.0  1.2  1.0  1.0   1.0
 [6,]  1.2  1.0  1.0  1.0  1.2  1.0  1.2  1.0  1.0   1.0
 [7,]  1.2  1.0  1.0  1.0  1.2  1.2  1.0  1.0  1.0   1.2
 [8,]  1.0  1.0  1.2  1.0  1.2  1.2  1.0  1.0  1.0   1.2
 [9,]  1.0  1.2  1.2  1.2  1.2  1.2  1.0  1.2  1.0   1.0
[10,]  1.0  1.2  1.2  1.0  1.0  1.0  1.2  1.0  1.0   1.0
[11,]  1.0  1.2  1.0  1.2  1.0  1.2  1.2  1.0  1.2   1.0
[12,]  1.2  1.2  1.2  1.0  1.2  1.2  1.0  1.2  1.2   1.2
[13,]  1.2  1.0  1.0  1.2  1.2  1.0  1.2  1.0  1.0   1.0
[14,]  1.2  1.0  1.2  1.0  1.2  1.0  1.0  1.0  1.2   1.0
[15,]  1.2  1.0  1.0  1.2  1.2  1.2  1.0  1.0  1.0   1.0
[16,]  1.0  1.2  1.2  1.2  1.2  1.0  1.2  1.0  1.0   1.2
[17,]  1.2  1.2  1.0  1.2  1.2  1.2  1.0  1.2  1.2   1.0
[18,]  1.2  1.0  1.0  1.0  1.2  1.2  1.0  1.2  1.2   1.2
[19,]  1.2  1.2  1.0  1.0  1.2  1.0  1.2  1.2  1.0   1.0
[20,]  1.2  1.0  1.2  1.0  1.0  1.2  1.0  1.2  1.0   1.2

x <- c(1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3)
f2(x, 20, 10)

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]  1.0  2.2  2.0  1.6  2.8  3.0  2.4  1.4  2.8   2.4
 [2,]  3.0  1.4  2.8  2.0  1.6  1.8  2.6  1.2  1.8   2.4
 [3,]  1.6  1.4  1.0  1.2  1.4  1.2  2.4  1.4  3.0   3.0
 [4,]  2.4  2.6  2.6  2.4  1.2  3.0  2.2  2.0  1.0   1.8
 [5,]  2.4  1.8  2.6  2.6  2.2  1.4  2.6  1.2  2.2   1.8
 [6,]  2.6  2.6  3.0  1.4  2.8  1.8  2.0  2.6  1.2   1.8
 [7,]  2.4  2.8  1.6  1.2  3.0  1.4  1.0  1.8  1.6   1.6
 [8,]  2.4  2.6  1.8  3.0  1.4  2.4  1.8  3.0  2.6   2.2
 [9,]  2.6  2.8  2.6  2.0  3.0  2.2  2.8  2.2  2.2   1.0
[10,]  1.4  2.6  3.0  3.0  2.6  2.4  1.4  2.2  2.2   1.0
[11,]  1.8  2.8  1.8  2.0  1.2  1.4  2.2  1.8  2.2   2.2
[12,]  1.2  1.6  1.0  3.0  1.8  3.0  2.0  2.0  2.4   1.2
[13,]  2.0  2.2  2.4  1.8  1.2  1.0  2.6  2.4  2.6   1.2
[14,]  2.2  2.6  3.0  1.6  2.4  1.6  2.2  1.0  2.2   2.2
[15,]  2.4  2.6  2.8  1.0  2.4  2.8  2.6  2.8  1.2   2.6
[16,]  1.6  3.0  3.0  2.2  1.2  2.6  2.2  1.0  2.4   1.6
[17,]  2.8  1.4  1.6  3.0  2.2  2.6  1.0  1.0  2.2   1.4
[18,]  1.4  2.2  1.8  2.6  1.2  3.0  2.4  2.4  2.6   2.0
[19,]  1.8  2.2  3.0  1.4  2.6  1.8  2.8  2.8  3.0   3.0
[20,]  1.4  1.4  2.6  1.2  2.8  3.0  2.0  1.0  2.2   2.8


来源:https://stackoverflow.com/questions/47924324/how-to-assign-a-set-of-numbers-randomly-to-a-matrix-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!