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?
Asked
Active
Viewed 87 times
1 Answers
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

PythonAteMyHamster
- 176
- 1
- 11
-
Thank you. however, I need ten sets of samples, not just one – Sep 16 '15 at 15:26
-
OK: **set.seed(1234); samples=list(); for (i in 1:10) samples[[i]]=m[sample(500,5),]** ... then access the results as samples[[1]] up to samples[[10]] – PythonAteMyHamster Sep 16 '15 at 15:29
-
@Max - try `replicate(10, m[sample(nrow(m), 5), ], simplify = FALSE)` – Rich Scriven Sep 21 '15 at 22:42