我有一些代码允许我从数据集中随机抽取两个样本,应用一个函数并重复该过程一定次数(请参阅下面来自相关问题的代码:How to bootstrap a function with replacement and return the output)。
示例数据:
> dput(a)
structure(list(index = 1:30, val = c(14L, 22L, 1L, 25L, 3L, 34L,
35L, 36L, 24L, 35L, 33L, 31L, 30L, 30L, 29L, 28L, 26L, 12L, 41L,
36L, 32L, 37L, 56L, 34L, 23L, 24L, 28L, 22L, 10L, 19L), id = c(1L,
2L, 2L, 3L, 3L, 4L, 5L, 6L, 7L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 16L, 17L, 18L, 19L, 20L, 21L, 21L, 22L, 23L, 24L,
25L)), .Names = c("index", "val", "id"), class = "data.frame", row.names = c(NA,
-30L))
代码:
library(plyr)
extractDiff <- function(P){
subA <- P[sample(nrow(P), 15, replace=TRUE), ] # takes a random sample of 15 rows
subB <- P[sample(nrow(P), 15, replace=TRUE), ] # takes a second random sample of 15 rows
meanA <- mean(subA$val)
meanB <- mean(subB$val)
diff <- abs(meanA-meanB)
outdf <- c(mA = meanA, mB= meanB, diffAB = diff)
return(outdf)
}
set.seed(42)
fin <- do.call(rbind, replicate(10, extractDiff(a), simplify=FALSE))
我不想抽取两个大小为 15 的随机抽取样本,而是抽取一个大小为 15 的随机抽取样本,然后在第一次随机抽取后提取数据集中剩余的 15 行(即subA
等于第一个随机抽取的样本15 个 obs,subB
将等于在 subA 被占用后剩余的 15 个 obs)。我真的不知道该怎么做。任何帮助将非常感激。谢谢!