[R] Selecting Random Subset From Matrix - retaining indices

Dieter Menne dieter.menne at menne-biomed.de
Sun Jan 29 15:31:14 CET 2006


Laura Quinn <laura <at> env.leeds.ac.uk> writes:

> I was wondering whether there is a way to select random samples from a
> data matrix, retaining the indexing for the rows and columns? 

Is this you are looking for? Dieter


# create data
n = 10
world = array(rnorm(n*n*n),c(n,n,n))
nsamples=12
# create indices
isample = matrix(sample(n,3*nsamples, replace=T),nrow=nsamples)
# use indexes to pick world samples
world[rbind(isample)]




More information about the R-help mailing list