Skip to content

Select exactly n elements of a vector

2 messages · Kort, Eric, Brian Ripley

#
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))
n<-1000
res<-numeric(n)
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.

Thanks,
Eric
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Mon, 12 Aug 2002, Kort, Eric wrote:

            
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).