简体   繁体   English

从n组数字中生成x个随机数

[英]Generate x random numbers from n groups of numbers

Assuming I have the following lists of numbers in R, how can I choose 6 random numbers by specifying the how many I need from each group? 假设我在R中具有以下数字列表,如何通过指定每个组中需要多少个数字来选择6个随机数?

r<-c(1,2,3,4,5,6,7,8,9,10)
b<-c(11,12,13,14,15,16,17,18,19,20)
y<-c(21,22,23,24,25,26,27,28,29,30)
g<-c(31,32,33,34,35,36,37,38,39,40)

How can I select, say 2 random numbers from r, 2 random numbers from b, 1 random number from y and 1 random number from g? 我该如何选择r中的2个随机数,b中的2个随机数,y中的1个随机数和g中的1个随机数?

I need my codes to be flexible enough to manually input how many numbers I need from each list. 我需要我的代码足够灵活以手动输入每个列表中需要多少个数字。 I also want the final output to be just the list of 6 random numbers, based on my selection. 根据我的选择,我还希望最终输出只是6个随机数的列表。

Put vectors into one list 将向量放入一个列表

foo <- list(
    c(1,2,3,4,5,6,7,8,9,10),
    c(11,12,13,14,15,16,17,18,19,20),
    c(21,22,23,24,25,26,27,28,29,30),
    c(31,32,33,34,35,36,37,38,39,40)
)

Sample x times 样品x次

If you don't mind replacement then generate another vector of draws (specifies from which list entry to sample) and iterate over this vector. 如果您不介意替换,则生成另一个绘制矢量(指定要从哪个列表条目进行采样)并遍历此矢量。

bar <- c(1, 1, 2, 2, 3, 4)
sapply(bar, function(x) sample(foo[[x]], 1))

Sample n times 采样n次

If you don't want replacement then you have to sample only once from each list entry. 如果您不想替换,则只需从每个列表条目中采样一次。 To do this you have to name your list entries with how many draws you want and iterate over the list (for each entry extract name and sample this number). 为此,您必须使用所需的绘制次数来命名列表条目,并在列表上进行迭代(对于每个条目,请提取名称并对此编号进行采样)。

names(foo) <- c(2, 2, 1, 1)
unlist(sapply(seq_along(foo), function(x) sample(foo[[x]], names(foo[x]))))

Sample 1 time 样品1次

Another solution is to generate vector of probabilities and pass it as prob argument in sample . 另一种解决方案是生成概率向量,并将其作为sample prob参数传递。

sample(unlist(foo), length(bar),
       prob = rep(table(bar) / length(bar) / lengths(foo), lengths(foo)))

Explanation: 说明:

How many samples from each entry we need: table(bar) 每个条目需要多少个样本: table(bar)

1 2 3 4 
2 2 1 1 

Assign each entry a probability: table(bar) / length(bar) 给每个条目分配一个概率: table(bar) / length(bar)

        1         2         3         4 
0.3333333 0.3333333 0.1666667 0.1666667 

Give each number it's probability to be sampled: rep(table(bar) / length(bar) / lengths(foo), lengths(foo)) 给每个数字采样的可能性: rep(table(bar) / length(bar) / lengths(foo), lengths(foo))

...

         2          2          2          2          2          2          2          2          2          2 
0.03333333 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333 
         3          3          3          3          3          3          3          3          3          3 
0.01666667 0.01666667 0.01666667 0.01666667 0.01666667 0.01666667 0.01666667 0.01666667 0.01666667 0.01666667 

...

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

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