# How to take a random sample from a matrix in R?

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.

For example, if we have a matrix called M that contains 100 elements and we want to sample 200 elements from M then we can use the below given command −

sample(M,200,replace=TRUE)

## Example 1

Following snippet creates a matrix −

M1<-matrix(rpois(40,2),ncol=2)
M1

The following matrix is created −

    [,1] [,2]
[1,]  4   1
[2,]  2   2
[3,]  1   1
[4,]  1   1
[5,]  3   3
[6,]  3   3
[7,]  0   1
[8,]  6   2
[9,]  2   3
[10,] 3   0
[11,] 2   1
[12,] 2   0
[13,] 2   1
[14,] 3   2
[15,] 4   1
[16,] 2   5
[17,] 2   0
[18,] 5   1
[19,] 1   2
[20,] 2   1

To sample 20 elements from matrix M1, add the following code to the above snippet −

M1<-matrix(rpois(40,2),ncol=2)
sample(M1,20)

## Output

If you execute all the above given snippets as a single program, it generates the following output −

 1 6 1 3 5 2 2 3 0 3 3 4 1 2 3 2 2 1 5 2


## Example 2

Following snippet creates a matrix −

M2<-matrix(rnorm(40),ncol=2)
M2

The following matrix is created −

        [,1]          [,2]
[1,]   0.48397719  -0.3434391
[2,]  -1.13421796  -0.3913274
[3,]   1.05965857  -1.0989064
[4,]   1.23304607  -1.3435612
[5,]   1.77637786   0.7310268
[6,]   0.41760502   1.3260546
[7,]  -0.08124738   0.8677471
[8,]  -0.15993339   0.1359703
[9,]  -0.82723524  -0.1186969
[10,]  1.41121197   0.0248495
[11,]  0.48756826   1.3339764
[12,] -0.02805691  -1.2450467
[13,]  0.55314428   0.8207469
[14,] -0.47900431   0.3231949
[15,] -0.02465099   0.4216422
[16,] -0.81648466   0.3326057
[17,] -0.97571912  -0.7905656
[18,]  0.51536039  -0.4992937
[19,] -0.70616764  -1.0087116
[20,]  0.80158965  -1.0430299

To sample 50 elements from matrix M2, add the following code to the above snippet −

M2<-matrix(rnorm(40),ncol=2)
sample(M2,50,replace=TRUE)

## Output

If you execute all the above given snippets as a single program, it generates the following output −

 -1.34356116 1.23304607 1.05965857 -0.82723524 -0.11869686 -0.11869686
 0.33260571 -0.82723524 0.13597027 -1.13421796 1.33397641 1.32605457
 0.73102677 1.77637786 0.42164215 1.77637786 0.42164215 -0.82723524
 -0.47900431 0.51536039 -1.09890635 -0.39132740 -1.34356116 1.32605457
 1.77637786 -1.24504668 0.02484950 -0.82723524 0.73102677 -0.08124738
 0.55314428 -0.97571912 -0.97571912 0.48397719 -0.02805691 1.32605457
 0.82074690 -0.02465099 -1.13421796 0.55314428 0.55314428 1.41121197
 0.42164215 -1.24504668 -1.00871161 0.82074690 -0.79056563 0.48756826
 0.86774712 1.33397641

## Example 3

Following snippet creates a matrix −

M3<-matrix(rpois(40,10),ncol=2)
M3

The following matrix is created −

     [,1] [,2]
[1,]   7   9
[2,]  14   8
[3,]  11   5
[4,]  11   8
[5,]   9   8
[6,]   6   9
[7,]  12  12
[8,]  17   5
[9,]  14   8
[10,] 13  11
[11,] 11   5
[12,] 11  13
[13,] 14  10
[14,] 12  15
[15,] 11  13
[16,] 11   7
[17,] 12   8
[18,] 11  11
[19,] 12  10
[20,]  6   9

To sample 50 elements from matrix M3, add the following code to the above snippet −

M3<-matrix(rpois(40,10),ncol=2)
sample(M3,50,replace=TRUE)

## Output

If you execute all the above given snippets as a single program, it generates the following output −

 14 8 15 17 9 9 8 12 11 9 6 10 14 10 6 8 9 9 14 13 11 5 8 11 11
 12 9 9 11 11 11 5 11 11 6 13 13 6 12 11 17 12 11 11 12 10 11 11 10 12