I am relatively new to R and I am looking to randomly sample from a dataframe containing a column with area values. How would I go about achieving this where I sample rows until the sum of the areas reach a certain value(or close to it)? I've tried using the code shown below from a previous question that's similar to mine, but the sum of the samples are not always within the range set in the code.
sample <- function(df) {
s1<- df[sample(rownames(df),1),]
s11 <- sum(s1$Area)
while (s11<43900000) {
rn2<- rownames(df[!(rownames(df) %in% rownames(s1)),])
nr<- df[sample(rn2,1),]
s11 <- sum(rbind(s1,nr)$Area)
if(s11>43800000){
break()
}
s1<-rbind(s1,nr)
}
return(s1)
}
Aucun commentaire:
Enregistrer un commentaire