0

I have a matrix in R, say 500 rows. I want to draw 10 samples of 5 rows each from this matrix, and the result of the sampling should be able to be replicated. How should I write the syntax?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Max
  • 1
  • 1

1 Answers1

0

Use set.seed() to make your results reproducible ...

> ### here's your matrix
> m = matrix(runif(1500),nrow=500)
>
> ### grab 5 rows using seed 1234
> set.seed(1234); rows=sample(500,5); m[rows,]
           [,1]       [,2]        [,3]
[1,] 0.04185728 0.99020375 0.541874639
[2,] 0.19666771 0.37688659 0.652112981
[3,] 0.54845545 0.66182218 0.418654421
[4,] 0.14608279 0.13103702 0.387722061
[5,] 0.43969737 0.02609232 0.003891448
>
> ### it's reproducible, see ...
> set.seed(1234); rows=sample(500,5); m[rows,]
           [,1]       [,2]        [,3]
[1,] 0.04185728 0.99020375 0.541874639
[2,] 0.19666771 0.37688659 0.652112981
[3,] 0.54845545 0.66182218 0.418654421
[4,] 0.14608279 0.13103702 0.387722061
[5,] 0.43969737 0.02609232 0.003891448