根据条件对n个对象采样x次

时间:2019-07-03 15:56:11

标签: r python-3.x sample sampling

我想从总共26次n次采样中抽取6个字母,以便每个字母在所有n次中正好采样3次。我将如何用R或python编写此代码?

编辑:我错过了一个关键标准,每套6个字母应该是唯一的。因此,每套6个字母都应采样而不更换。

1 个答案:

答案 0 :(得分:1)

根据您的条件,您最多可以有13个样本,即26 * 3 / 6 = 13。这是获得它们的一种方法-

set.seed(2)
m <- matrix(sample(rep(letters, 3)), nrow = 6)
m
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,] "o"  "j"  "y"  "a"  "s"  "a"  "j"  "g"  "p"  "x"   "x"   "p"   "y"  
[2,] "c"  "h"  "l"  "e"  "l"  "z"  "m"  "f"  "x"  "n"   "d"   "v"   "o"  
[3,] "r"  "g"  "z"  "m"  "h"  "p"  "q"  "v"  "v"  "a"   "e"   "c"   "o"  
[4,] "m"  "l"  "b"  "w"  "k"  "n"  "f"  "s"  "b"  "u"   "d"   "h"   "y"  
[5,] "r"  "u"  "i"  "u"  "w"  "e"  "t"  "f"  "r"  "w"   "t"   "t"   "z"  
[6,] "q"  "q"  "n"  "i"  "g"  "s"  "k"  "k"  "c"  "j"   "i"   "d"   "b"  

table(m)
m
a b c d e f g h i j k l m n o p q r s t u v w x y z 
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3