[R] Select exactly n elements of a vector

Kort, Eric Eric.Kort at vai.org
Mon Aug 12 19:39:06 CEST 2002

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.

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