[R] Vectorizing sample()

Duncan Murdoch murdoch at stats.uwo.ca
Fri Nov 7 18:40:05 CET 2008


On 11/7/2008 12:00 PM, Stephen Collins wrote:
> 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.

You might want to transform runif instead of using sample().  For 
example, if you want to generate M integers from 1:n_i, where n_i varies 
from sample to sample, you could use

gen <- ceiling(runif(M, 0, n))

(where n is a vector of length M giving the upper limits).

Duncan Murdoch


> 
> 
> 
> Regards,
>  
> 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.



More information about the R-help mailing list