I have some code that allows me to take two randomly drawn samples from a dataset, apply a function and repeat the procedure a certain number of times (see below code from associated question: How to bootstrap a function with replacement and return the output).
Example data:
> 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))
Code:
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))
Rather than taking TWO randomly drawn samples of size 15, I would like to take one randomly drawn sample of size 15, then extract the remaining 15 rows in the dataset after the first random draw has been taken (i.e. subA
would equal the first randomly drawn sample of 15 obs, subB
would equal the remaining 15 obs after subA has been taken). I am really not sure how to go about doing this. Any help would be really appreciated. Thanks!