简体   繁体   English

R从矩阵中随机抽取哪些样本?

[英]R which samples were drawn randomly from matrix?

I am trying to determine which columns were sampled from a matrix randomly sampled within each row. 我试图确定哪些列是从每一行中随机抽样的矩阵中抽样的。 The function sample does not appear to have the ability to tell you which locations were actually sampled. 函数样本似乎没有能力告诉您实际采样的位置。 Now, a simple matching routine can solve the problem if all values are unique. 现在,如果所有值都是唯一的,那么简单的匹配例程就可以解决问题。 However, they are not in my case, so this will not work. 但是,就我而言,它们不是,所以这行不通。

x <- c(2,3,5,1,6,7,2,3,5,6,3,5)
y <- matrix(x,ncol=4,nrow=3)
random <- t(apply(y,1,sample,2,replace=FALSE))

y ÿ

   [,1] [,2] [,3] [,4]
[1,]    2    1    2    6
[2,]    3    6    3    3
[3,]    5    7    5    5

random 随机

     [,1] [,2]
[1,]    2    6
[2,]    3    3
[3,]    5    5

With repeated values in the original matrix, I cannot tell if random[1,1] was sampled from column 1 or column 3, since they both have a value of 2. Hence, matching won't work here. 对于原始矩阵中的重复值,我无法确定是否从第1列或第3列中采样了random [1,1],因为它们的值均为2。因此,匹配在这里不起作用。

Accompanying the matrix "random" I would also like a matrix that gives the column from which each value was sampled, in an identically sized matrix. 伴随矩阵“随机”,我还想要一个矩阵,该矩阵以大小相同的矩阵给出从中采样每个值的列。 For example, such as: 例如,例如:

     [,1] [,2]
[1,]    1    4
[2,]    1    3
[3,]    3    4

Thanks! 谢谢!

You need to save your random selections from sample separately so you don't have to worry about matching later. 您需要分别保存sample的随机选择,因此您不必担心以后进行匹配。 Eg, using y again: 例如,再次使用y

y
#     [,1] [,2] [,3] [,4]
#[1,]    2    1    2    6
#[2,]    3    6    3    3
#[3,]    5    7    5    5

set.seed(42)
randkey <- t(replicate(nrow(y),sample(1:ncol(y),2)))
#     [,1] [,2]
#[1,]    4    3
#[2,]    2    3
#[3,]    3    2

random <- matrix(y[cbind(c(row(randkey)), c(randkey))], nrow(y))
#     [,1] [,2]
#[1,]    6    2
#[2,]    6    3
#[3,]    5    7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM