Laura Quinn
2006-Jan-28 18:27 UTC
[R] Selecting Random Subset From Matrix - retaining indices
Hello, I was wondering whether there is a way to select random samples from a data matrix, retaining the indexing for the rows and columns? I have looked at using the sample() function. Applied directly to my matrix this returns a vector of absolute values but the indices are lost, alternatively I can select a random sample from a length equal to the number of elements in the matrix and then translate each number into an element withing the array but this seems to require a lot of work to ascertain the position and value of each element. Is there a better way of performing this operation? Further to my earlier query I am hoping to pick a random selection of grid points (with (x,y,z) coords) from a 3d map matrix. Thanks in advance. Laura Quinn Institute of Atmospheric Science School of Earth and Environment University of Leeds Leeds LS2 9JT tel: +44 113 343 1596 fax: +44 113 343 6716 mail: laura at env.leeds.ac.uk
Dieter Menne
2006-Jan-29 14:31 UTC
[R] Selecting Random Subset From Matrix - retaining indices
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)]