[R] Select exactly n elements of a vector
ripley at stats.ox.ac.uk
Mon Aug 12 19:49:08 CEST 2002
On Mon, 12 Aug 2002, Kort, Eric wrote:
> In discussing permutation tests, _Modern Applied Statistics With S Plus_ suggests this (IMHO) rather elegant approach to generating a distribution of t-tests:
> d<- ...some data...
> ttest <- function(x) mean(x)/sqrt(var(x)/length(x))
> for(i in 1:n) res[i] <- ttest(x<-d*sign(runif(10)-0.5)
> The problem is that the sign(runif) results in random permutation
of the observations into two groups of random size. For my analysis,
however, I want to fix the size of the two groups. So I wonder if
someone can point me toward an elegant way to randomly split a vector
into two groups of specified sizes. Or, similarly, randomly select
a specific number of elements from a vector.
That's part of sample(). sample(10,4, TRUE) selects 4 elements from 1:10.
(and the negation will extract the other sample).
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