I'm still relatively new to R and am having trouble figuring out how to randomly allocate data based on a known proportion. Or, if there is another way to crosswalk this data - I'd love to learn that, also.
I have health data at the zip code level that I need to allocate to the census tract level. My health data is in this form:
patzip dx_prin patid
95437 L03111 0123
95437 L03111 0124
95437 L02112 0125
95437 L03114 0126
95437 L01001 0127
95437 L02112 0128
...
where each row is a unique patient visit and "patzip" is the patient zip code.
I have a crosswalk file from HUD that has all the California zip codes and the proportion of residential addresses within that zipcode that fall within each California census tract. That data is in this form:
zip tract res_ratio
95437 06045010300 0.2113775795
95437 06045010400 0.4151701060
95437 06045010200 0.0072504183
95437 06045010500 0.2339096486
95437 06045011002 0.1322922476
...
What I'd like to do is randomly allocate each person in my health dataset to a census tract based on the "res_ratio" value. So, 21% of the patients with zip code 95437 would go to the first census tract, 41.5% to the second census tract, and so on. I feel like the answer to this problem is likely simple, but I've been stuck for a few days. I'd really appreciate any help or advice!