R采样直到满足条件

R sample until a condition is met

所以我有以下数据框:

structure(list(V1 = c(45L, 17L, 28L, 26L, 18L, 41L, 26L, 20L, 
23L, 31L, 48L, 23L, 32L, 18L, 30L, 11L, 26L)), .Names = "V1", row.names = c("24410", 
"26526", "26527", "43264", "63594", "125630", "148318", "245516", 
"269500", "293171", "301217", "400294", "401765", "520084", "545501", 
"564914", "742654"), class = "data.frame")

行名代表地块,V1 显示我可以从中提取的每个地块的示例数。 我想要的是从每个包裹中抽取与可用示例数量成比例的样本,最终下落以每个包裹总共 400 个示例结束。这个想法是不要对一个地块相对于其他地块进行过度采样。

正在进行采样的数据集是 here

到目前为止,代码如下所示:

df <- read.csv('/data/samplefrom.csv')
df.training <- data.frame()
n <- 400

for(crop in sort(unique(df$code_surveyed))){
  for (bbch_stage in sort(unique(df$bbch))) {
    df.int <- df[df$bbch==bbch_stage & df$code_surveyed == crop,]
    df.int <- df.int[!is.na(df.int$name),]
    rawnum <- nrow(df[df$bbch==bbch_stage & df$code_surveyed == crop,])
    if(rawnum >= n){
      df.bbch.slected<-df[df$bbch==bbch_stage & df$code_surveyed == crop,]
      df.bbch.slected.sampled<-df.bbch.slected[sample(nrow(df.bbch.slected), n),] #(round(n_bbch*length(which(df$bbch==bbch_stage))))), ]
      df.training<-rbind(df.training,df.bbch.slected.sampled)
    }

  }
}

这样做的目的是为每种裁剪 + bbch_stage 组合随机采样 400 个示例(将其理解为复合变量)。这一切都很好,但我希望能够控制示例来自哪个包裹(变量 objectid)。本质上是采样时的额外过滤步骤。

我已经用 whilerepeat 语句以及 devtools 中的 stratified 函数尝试了几次,但是 none似乎产生了我想要的东西。

好吧,经过一些起起落落,我走到了这一步:

df.training<-data.frame()
for (crop in unique(df$code)) {
  df.crop.slected<-df[df$code==crop,]
  df.crop.slected.sampled <- data.frame()
  while(nrow(df.crop.slected.sampled) < 400){
    for(parcel in 1:length(unique(df.crop.slected$objectid))){
      df.crop.slected.pacel <- df.crop.slected[df.crop.slected$objectid == unique(df.crop.slected$objectid)[parcel],]
      df.crop.slected.pacel <- df.crop.slected.pacel[sample(nrow(df.crop.slected.pacel), 1), ]
      if(! df.crop.slected.pacel$name %in% df.crop.slected.sampled$name){
        df.crop.slected.sampled <- rbind(df.crop.slected.sampled, df.crop.slected.pacel)
      }

    }
  }
  df.training<-rbind(df.training,df.crop.slected.sampled)
}

虽然肯定不是最优雅的,但它完成了工作。如果有人可以指导我使用分层抽样功能,以更简单的方式实现这一点,我将非常感激。