Random Sampling - Matrix

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

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 taken N samples randomly.

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

Additional Information

By accident, I also mean the distribution inside the "matrix space", for example. if I make a selection of 4 points, I don’t want to have 4 neighboring points as a result, I want them to be distributed in the β€œmatrix space”.

Knowing the position (row, column) in the matrix where I selected random points would also be important.

+3
r random-sample
source share
2 answers

There is a very simple way to draw a matrix, which works if you understand that R is a matrix inside of itself as a vector.

This means that you can use sample directly on your matrix. For example, suppose you want to sample 10 points with a replacement:

 n <- 10 replace=TRUE 

Now just use sample in 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 it be divided into two steps. Step 1 - create an index of sample positions, and step 2 - find these 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.

+12
source share

The sample seems best to you. To get 1000 random positions, you can do something like:

 rows = sample(1:nrow(dataset), 1000, replace = TRUE) columns = sample(1:ncol(dataset), 1000, replace = TRUE) 

I think it gives what you want, but of course I'm wrong.

The extraction of elements from the matrix can be performed as follows:

 random_sample = mapply(function(row, col) return(dataset[row,col]), row = rows, col = columns) 

Sampling strategies

In the comments, you say that your sample should be distributed. Random sampling has no guarantee that there will be no clusters due to its random nature. There are several more sampling schemes that may be interesting to study:

  • Regular sampling, skipping fluke and just sample regularly. Samples the entire matrix space uniformly, but no chance.
  • Stratified random sampling, you divide the matrix space into a regular subset, and then arbitrarily make choices in these subsets. Represents a combination between random and regular.

To check if your random sample gives good results, I would repeat the random sample several times and compare the results (since I assume that the sample will be introduced for another analysis?).

+4
source share

All Articles