R中多行的多次采样

时间:2015-09-16 14:58:09

标签: r sampling

我在R中有一个矩阵,比如500行。我想从这个矩阵中抽取10个样本,每行5行,并且应该能够复制抽样结果。我该如何编写语法?

1 个答案:

答案 0 :(得分:0)

使用set.seed()使结果可重现......

> ### 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