Bigs<-data.frame(Type="Bigs", Area=rbinom(3,100,rbeta(10,1,1)), Species1=rbinom(3,1,.5), Species2=rbinom(3,1,.5), stringsAsFactors = F);
Smalls<-data.frame(Type="Smalls",Area=rbinom(1000,20,rbeta(1000,1,1)),Species1=rbinom(20,1,.5),Species2=rbinom(20,1,.5),stringsAsFactors = F);
L<-lapply(1:nrow(Bigs),
function(i){
samplesmalls<-c();
x=Bigs$Area[i];
while(x>0&x>min(Smalls$Area)){
drawsmall<-sample((1:nrow(Smalls))[Smalls$Area<=x],1)
samplesmalls<-c(samplesmalls,drawsmall)
x<-x-Smalls$Area[drawsmall]
}
samplesmalls
})
do.call(rbind,lapply(1:length(L),function(i){cbind(toreplace=i,Smalls[L[[i]],])}))
toreplace Type Area Species1 Species2
251 1 Smalls 19 1 1
502 1 Smalls 9 0 1
616 1 Smalls 12 0 1
163 1 Smalls 11 0 1
81 1 Smalls 2 1 0
609 1 Smalls 8 1 0
853 1 Smalls 0 1 0
702 1 Smalls 3 0 1
451 2 Smalls 9 1 1
432 2 Smalls 5 0 0
643 2 Smalls 1 0 1
391 2 Smalls 0 1 1
259 2 Smalls 0 1 1
905 2 Smalls 1 1 0
35 3 Smalls 10 1 0
727 3 Smalls 17 1 1
640 3 Smalls 8 1 0
357 3 Smalls 0 1 0
900 3 Smalls 4 1 0
217 3 Smalls 3 1 0
771 3 Smalls 4 1 1
647 3 Smalls 1 1 1
351 3 Smalls 5 1 1
412 3 Smalls 6 0 0
639 3 Smalls 2 1 1
183 3 Smalls 0 0 1
962 3 Smalls 0 0 1
567 3 Smalls 0 1 1
212 3 Smalls 1 0 0