简体   繁体   中英

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. Participants shouldn't be repeated/duplicated in any means. Participants are identified by unique IDs.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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