Search code examples
rselectrandomsumsample

R randomly select rows until varying sum is met


I am interested in comparing the some values between different sized patches but want to randomly select small patches until their area equals that of a randomly selected large patch. I would then compare the unique count of species between this selection of small patches and the single large patch.

For instance

>df
ID     Type        Area        Species1     Species2
1      Big          50            1            1
2      Big          100           1            0
3      Small        25            0            1
4      Small        50            1            1
5      Small        25            1            1

I would then like to randomly select small patches until their Area sums equal a randomly chosen big patch. Then I would like to compare the unique count of species between this selection and the randomly chosen big patch of equal size. For example

If patch 1 is chosen and the random selector could select either patch 4, or 3+5. The comparison would then be between Unique Count Patch1 =2, Unique Count Patch4 =2, or Unique Count 3+5= 2.

I hope this makes sense. If there any questions I would be happy to reply/update this. Thank you for your help.


Solution

  • 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