如何随机分配一组数字到R中的矩阵 [英] How to assign a set of numbers randomly to a matrix in R

查看:120
本文介绍了如何随机分配一组数字到R中的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为矩阵随机分配固定数字,例如"x","y","z"等.我怎样才能做到这一点?我确实进行了搜索,但是他们都解释了如何制作一个具有随机数的矩阵.但是我的数字不是随机的.我知道我要在矩阵中输入什么数字,我不知道如何将它们随机分配给矩阵.而且我无法为此编写任何代码行来纠正它.

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.

这里是一个例子.假设我有数字1和1.2.我想生成一个20 * 10的矩阵,其元素从1和1.2中随机选择.矩阵示例如下:

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

每行至少应出现一次.

非常感谢

推荐答案

解决方案

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

说明

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

创建一个随机排序的10个样本,以确保其中一个元素为1,其中一个元素为1.2,其他8个元素则从1和1.2中随机选择.

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)))))

这样做20次,然后将答案转置为您想要的尺寸.

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

在进行其他评论之后,您似乎需要能够完成两项不同的工作:

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

  1. 创建一个n x m的矩阵,该矩阵随机填充x的值,但要遵守这样的约束:结果矩阵的每一行至少包含x的每个元素.
  2. 创建一个n x m的矩阵,该矩阵随机地填充x的值,但要受制于所得矩阵的每一行都具有多个唯一值的约束.
  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()完成了我对任意xnm的原始回答(对应于上面的数字1). f2()完成新请求(对应于上面的数字2);但是,请注意,可能有更好的方法来完成此任务,并且此方法(while循环)可能会花费任意时间,具体取决于xnm和机会的值.以下是函数的示例调用:

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

这篇关于如何随机分配一组数字到R中的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆