R中data.frame的嵌套采样

问题描述

在下面的数据框 <div class="form-group"> @Html.LabelFor(model => model.Tag.Name,htmlAttributes: new { @class = "control-label col-md-2",style = "border: 1px solid red;" }) <div class="col-md-3" style="border: 1px solid red; padding-right:0;"> @Html.EditorFor(model => model.Tag.Name,new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Tag.Name,"",new { @class = "text-danger" }) </div> <div class="col-md-7" style="border: 1px solid red;"> <input type="submit" value="Add Tag" class="btn btn-default" style="border: 1px solid red;" /> </div> </div> 中,有p个唯一的学区名称757)和dname个唯一的学校名称5210)。

我想知道如何从R中的40 sname个(区)中抽取126个sname(学校)吗?

因此,在最终示例(例如dname)中,X必须返回:dim(table(X$dname,X$sname))

从某种意义上说,这是> [1] 40 126,所以我可以接受任何软件包。

multi-stage sampling

解决方法

我想您可以尝试以下代码来multi-stage sampling

unq_dname <- unique(p$dname)
repeat {
  out <- subset(p,dname %in% sample(unq_dname,40))
  if (length(unique(out$sname)) == 126) break
}

,您可以通过以下方式检查尺寸

dim(with(out,table(dname,sname)))