Untitled

By: a guest on May 28th, 2012  |  syntax: None  |  size: 0.93 KB  |  hits: 14  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. How do I select a sample of rows at random with repetition from a matrix in R?
2. X[sample(nrow(X),size=5,replace=TRUE),]
3.
4. X[sample(nrow(X),size=5,replace=FALSE),]
5.
6. x <- matrix(1:1000, nrow=100)
7.
8. x[sample(1:100, 5, replace=TRUE), ]
9.      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
10. [1,]   19  119  219  319  419  519  619  719  819   919
11. [2,]   51  151  251  351  451  551  651  751  851   951
12. [3,]   42  142  242  342  442  542  642  742  842   942
13. [4,]   48  148  248  348  448  548  648  748  848   948
14. [5,]   73  173  273  373  473  573  673  773  873   973
15.
16. x[sample(1:100, 5, replace=FALSE), ]
17.      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
18. [1,]   64  164  264  364  464  564  664  764  864   964
19. [2,]   67  167  267  367  467  567  667  767  867   967
20. [3,]   20  120  220  320  420  520  620  720  820   920
21. [4,]   17  117  217  317  417  517  617  717  817   917
22. [5,]    6  106  206  306  406  506  606  706  806   906