[R] Vectorizing sample()

From: Stephen Collins <Stephen_Collins_at_aon.com>
Date: Fri, 07 Nov 2008 11:00:13 -0600

I am simulating sickness among a group of families. Part of the task is to randomly draw who in the family will be sick, randomly drawing from family ID's where Dad =1, Mom = 2, Kid1 = 3, Kid2 = 4., etc. My census of Dads is of the form shown below.

Dad_ID  Spouse (Y=1;N=0)                #Kids #People_Becoming_Sick
1               1                               0               1
2               0                               2               2
3               1                               0               2
4               1                               3               3

The end output needed is if 3 people in a family are to be sick, was it the dad and two kids, with random family ID's = {1,3,4}, or the mom, dad, and one kid, with random family ID's = {2,1,4}, etc.. The complication is that length of the family ID's to choose from and the associated sampling probabilities -- changes with each family. I could loop through the Dads, from i in 1:nrow(census), but is there a way I could vectorize sample() to get at the same objective?

My attempts to use the apply-based functions have dead ended. Other ideas to vectorize this problem are warmly welcomed.


Stephen Collins, MPP | Analyst
Health & Benefits | Aon Consulting
200 East Randolph, Suite 900, Chicago, IL Tel: 312-381-2578 | Fax: 312-381-0136
Email: stephen_collins_at_aon.com  

Aon Consulting selected by the readers of Business Insurance as the “Best Employee Benefit Consulting Firm” in 2006, 2007, and 2008  

NOTE: The information contained in this transmission, including any attachment(s) is only for the use of the intended individual(s) or entity, and may contain information that is privileged and confidential. If the reader of this message is not an intended recipient, you are hereby notified that any dissemination, distribution, disclosure, or copying of this information is unauthorized and strictly prohibited. If you have received this communication in error, please contact the sender immediately by reply email and destroy all copies of the original message.  

        [[alternative HTML version deleted]]

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 07 Nov 2008 - 17:04:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 07 Nov 2008 - 18:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive