Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

random sampling - matrix

How can I take a sample of n random points from a matrix populated with 1's and 0's ?

a=rep(0:1,5)
b=rep(0,10)
c=rep(1,10)
dataset=matrix(cbind(a,b,c),nrow=10,ncol=3)

dataset
      [,1] [,2] [,3]
 [1,]    0    0    1
 [2,]    1    0    1
 [3,]    0    0    1
 [4,]    1    0    1
 [5,]    0    0    1
 [6,]    1    0    1
 [7,]    0    0    1
 [8,]    1    0    1
 [9,]    0    0    1
[10,]    1    0    1

I want to be sure that the positions(row,col) from were I take the N samples are random.

I know sample {base} but it doesn't seem to allow me to do that, other methods I know are spatial methods that will force me to add x,y and change it to a spatial object and again back to a normal matrix.

More information

By random I mean also spread inside the "matrix space", e.g. if I make a sampling of 4 points I don't want to have as a result 4 neighboring points, I want them spread in the "matrix space".

Knowing the position(row,col) in the matrix where I took out the random points would also be important.

like image 211
Gago-Silva Avatar asked Feb 02 '12 09:02

Gago-Silva


People also ask

What is a sampling matrix?

Simply put, a sampling matrix is the set of demographic characteristics you aim to see reflected in a sample of people. It is one way to increase the likelihood that your research results will generalize more accurately to your population of interest.

How do you randomly sample a matrix?

To take a random sample from a matrix in R, we can simply use sample function and if the sample size is larger than the number of elements in the matrix replace=TRUE argument will be used.

How do I select a random row from a matrix in R?

To randomize rows of a matrix in R, we can use sample function along with nrow function to get the random rows and then subset the matrix with single square brackets.


1 Answers

There is a very easy way to sample a matrix that works if you understand that R represents a matrix internally as a vector.

This means you can use sample directly on your matrix. For example, let's assume you want to sample 10 points with replacement:

n <- 10
replace=TRUE

Now just use sample on your matrix:

set.seed(1)
sample(dataset, n, replace=replace)
 [1] 1 0 0 1 0 1 1 0 0 1

To demonstrate how this works, let's decompose it into two steps. Step 1 is to generate an index of sampling positions, and step 2 is to find those positions in your matrix:

set.seed(1)
mysample <- sample(length(dataset), n, replace=replace)
mysample
 [1]  8 12 18 28  7 27 29 20 19  2

dataset[mysample]
 [1] 1 0 0 1 0 1 1 0 0 1

And, hey presto, the results of the two methods are identical.

like image 63
Andrie Avatar answered Sep 29 '22 07:09

Andrie