Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

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?

share|improve this question

3 Answers

up vote 7 down vote accepted

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.

share|improve this answer

Dason's thought, implemented in R:

sample <- split(sample(datapoints), rep(1:(length(datapoints)/10+1), each=10))
sample[[13]] # the thirteenth sample
share|improve this answer
(+1) Really neat R code. Of note, it will not work if $n$ is odd. – chl Oct 31 '10 at 22:24
@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. – conjugateprior Nov 1 '10 at 8:44
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). – chl Nov 1 '10 at 9:05
@chl Damn! You're quite right. – conjugateprior Nov 1 '10 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...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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