简体   繁体   English

如何从R中的向量中绘制N个随机样本?

[英]How to draw N random samples from a vector in R?

I have a vector with 663 elements. 我有一个663元素的向量。 I would like to create random samples from the vector equal to the length of the vector (ie 663). 我想从向量中创建等于向量长度的随机样本(即663)。 Said differently, I would like to take random samples from all possible orderings of the 663 elements. 换句话说,我想从663个元素的所有可能排序中随机抽取样本。 My goal is to create a data frame of the N random samples or randomly selected permutations. 我的目标是创建N个随机样本或随机选择的排列的数据帧。

I have tried the following: 我尝试过以下方法:

library(combinat)
perms <- as.data.frame(permn(1:663))

Since there are so many possible permutations, I would receive an error message. 由于有很多可能的排列,我会收到一条错误消息。

My next idea would be to create a data frame with as many rows as I would like samples/permutations and as many variables as elements (ie 663) and use a function like sapply() with sample() . 我的下一个想法是创建一个数据框,其中包含与我想要的样本/排列一样多的行以及与元素一样多的变量(即663),并使用类似sapply()的函数和sample() However, I don't think this approach is that efficient. 但是,我不认为这种方法是有效的。

I have also tried: 我也尝试过:

samples <- replicate(100, table(sample(1:663, 663,replace = F))) 

but I just get a data frame with 100 columns of ones. 但我只得到一个包含100列数据的数据框。

replicate will work replicate将工作

a <- 1:663 #vector of 663 elements
perms <- as.data.frame(replicate(100, sample(a)))

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

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