[R] sampling without repetition

Duncan Murdoch dmurdoch at pair.com
Tue Nov 18 02:31:52 CET 2003


On 17 Nov 2003 20:07:08 -0500, you wrote:


>Sorry about that. I wanted to select 3 disjoint sets from a supplied
>vector of numbers. My initial example had 
>
>r <- 1:300
>
>but there is no guarantee that r will contain a consecutive sequence of
>numbers.

It's still not clear whether the 3 sets should be of fixed size or
random size, and whether they should cover all of r or just part of
it.  Thomas' solution gave you a random partition.  If you just want 3
non-overlapping samples, each of size n, then do something like:

indices <- sample(1:length(r), size=3*n)
sample1 <- r[indices[1:n]]
sample2 <- r[indices[(n+1):(2*n)]]
sample3 <- r[indices[(2*n+1):(3*n)]]

Duncan Murdoch




More information about the R-help mailing list