Selecting Random Subset From Matrix - retaining indices

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Selecting Random Subset From Matrix - retaining indices

Laura Quinn-2
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: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Selecting Random Subset From Matrix - retaining indices

Dieter Menne
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)]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html