简体   繁体   English

R中的多重随机选择

[英]Multiple random selection in R

I'm aware that similar questions have been asked before, but I haven't found an answer to exactly what I need. 我知道以前也曾问过类似的问题,但是我没有找到确切的答案。 It seems like a simple solution I'm missing. 似乎是我缺少的简单解决方案。

I have a sample of approximately 20,000 participants and would like to randomly select 2500 from this sample to receive gift cards, and another unique 2500 (who aren't in the first group) to receive cash allowance. 我有一个大约20,000名参与者的样本,并希望从该样本中随机选择2500个以接收礼品卡,并另外一个独特的2500名(不在第一组中)接收现金津贴。 Participants shouldn't be repeated/duplicated in any means. 参加者不应以任何方式重复/重复。 Participants are identified by unique IDs. 参与者由唯一的ID标识。

I create indices for each row that represents participants (this step could be avoided, I believe). 我为代表参与者的每一行创建索引(我相信可以避免此步骤)。

Npool=1:dim(pool_20K)[[1]]
giftcards=sample(Npool,2500)

-- how do I create the cash allowance group so they are unique participants and do not include the ones selected for giftcards? -如何创建现金津贴组,使其成为唯一的参与者,并且不包括为礼品卡选择的现金津贴?

After, I would combine indices with the data 之后,我将索引与数据结合起来

giftcards_ids=pool_20K[giftcards, ]

Any insight? 有见识吗? I feel like I'm complicating a fairly simple problem. 我觉得我正在使一个相当简单的问题复杂化。

Thanks in advanced!! 提前致谢!!

Shuffle the entire thing and then select subsets: 随机播放整个内容,然后选择子集:

shuffled.indices = sample(nrow(pool_20K))

giftcards = shuffled.indices[1:2500]
cash = shuffled.indices[2501:5000]

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

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