[R] Selecting a random sample

Prof Brian D Ripley ripley at stats.ox.ac.uk
Mon May 22 21:37:36 CEST 2000


On Mon, 22 May 2000, Scot W McNary wrote:

> 
> Hi,
> 
> I'm trying to learn R and get an errand done at the same time.  I have a
> list of cases with ids numbered sequentially 1 to 403.  I want to select
> at random exactly 20 without replacement.  Here's what I've tried so far:
> 
> > ids<-(1:403)
> > pick<-runif(403)
> > picklist<-as.data.frame(cbind(ids,pick))
> > who<-subset(picklist, pick<20/403)
> > who
> 
> This works, but I can have anywhere from 16 to 32 cases in the subset
> 'who', due to random variability in the runif generator.  What's a more
> efficient (and general) way to extract a subset of exactly n from a total
> sample N?

sample(N, n, replace=F)

Easy?  See ?sample for variations on the theme.

By the way, seq(N)[runif(N) < n/N] would have done it your way in
simpler code, and 

seq(N)[rank(runif(N)) <= n]

is another general solution.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list