I've got a large set of data (20,000 data points), from which I want to take repeated samples of 10 data points. However, once I've picked those 10 data points, I want them to not be picked again.

I've tried using the sample function, but it doesn't seem to have an option to sample without replacement over multiple calls of the function. Is there a simple way to do this?


4 Answers 4


You could call sample once on the entire data set to permute it. Then when you want to get a sample you could grab the first 10. If you want another sample grab the next 10. So on and so forth.


Dason's thought, implemented in R:

sample <- split(sample(datapoints), rep(1:(length(datapoints)/10+1), each=10))
sample[[13]] # the thirteenth sample
  • $\begingroup$ (+1) Really neat R code. Of note, it will not work if $n$ is odd. $\endgroup$
    – chl
    Oct 31, 2010 at 22:24
  • $\begingroup$ @chl Thanks! But I think it will work. The task was to give samples of size 10 from a set of datapoints. Assume n = length(datapoints). The code gives the maximum number (n %/% 10) of such samples. The first corner case is n<10 (anyway ruled out in the problem statement by describing the dataset as 'large', i.e. n>10). In that case you get the datapoints back and a warning (not an error). The second corner case is if there are dangling elements (when n %% 10 != 0). Then you get as many samples as possible and a warning (not an error). Odd n situations are subsumed in one of these two cases. $\endgroup$ Nov 1, 2010 at 8:44
  • $\begingroup$ It seems the first element of the list is of length 11, not 10, and sum(unlist(lapply(sample, length))) return the length of datapoints (which I set to 1001). $\endgroup$
    – chl
    Nov 1, 2010 at 9:05
  • $\begingroup$ @chl Damn! You're quite right. $\endgroup$ Nov 1, 2010 at 9:37

This should work:

x <- rnorm(20000)
x.copy <- x
samples <- list()
i <- 1
while (length(x) >= 10){
    tmp <- sample(x, 10)
    samples[[i]] <- tmp
    i <- i+1
    x <- x[-match(tmp, x)]

table(unlist(samples) %in% x.copy)

However, I don't think that's the most elegant solution...


@conjugateprior Your answer is going in the right direction. But at least for my current R-version 3.4.3, it does not work. However with some tweaking it works:

mysample<-split(sample(length(datapoints)), 1:10)
mysample[[9]] ## the ninth sample

Since I cant comment yet I chose to answer here.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.