如何从R中的矩阵中重复选择行样本? [英] How do I select a sample of rows at random with repetition from a matrix in R?
本文介绍了如何从R中的矩阵中重复选择行样本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从R的矩阵中随机选择重复的行样本?
How do I select a sample of rows at random with repetition from a matrix in R?
所以请清楚,我将从一个100行的矩阵开始,然后我可以从其中的5行中选择一个新的矩阵.我希望选择是否进行替换或不进行替换.
So do be clear, I would start with a matrix of, for example, 100 rows and I would be able to select 5 of those rows and make a new matrix. I would want the option of doing this either with or without replacement.
推荐答案
在具有replace=TRUE
或replace=FALSE
的行上使用sample
.
如果X
是您的原始矩阵,则
If X
is your original matrix then
X[sample(nrow(X),size=5,replace=TRUE),]
或
X[sample(nrow(X),size=5,replace=FALSE),]
应该工作. (如果先选择示例:s <- sample(...)
然后选择子集:newmat <- X[s,]
,则可能更易读)
should work. (It may be more readable if you choose the sample first: s <- sample(...)
and then subset: newmat <- X[s,]
)
这篇关于如何从R中的矩阵中重复选择行样本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文