在R中创建一个样本矩阵

时间:2014-10-10 14:18:44

标签: r matrix sample sample-data

我有一个概率分布X,我想创建100个观测样本: 我使用sample(X,size=100,replace=TRUE)我想在100,1000,10000个样本上绘制样本均值PDF,因此我尝试使用matrix(sample(X,size=100,replace=TRUE),nrow=100,ncol=100)创建观察矩阵,但它会在所有列中生成相同的样本。关于如何为每列创建新样本的任何想法?

1 个答案:

答案 0 :(得分:3)

这是怎么回事?用你的样本调用替换rnorm。这将为每列采用新样本

replicate(3,rnorm(10))

#                          [,1]                     [,2]                    [,3]
# [1,] -0.439366440511456290974  0.349113310500896667499  2.10467702915785226381
# [2,]  0.788892611945899879800  0.572377925929974273878  0.92566383997665424577
# [3,]  0.098359807623723205516 -0.642162545019581476602  0.28636140673186011307
# [4,] -3.063133170307587249681  1.322694510750672014510  0.66340500173312999532
# [5,]  0.255018412772398617161  1.492588176987205361712  1.11444057062233659039
# [6,] -1.069621910039232570711 -1.460604130070508821504 -0.81534768620081377044
# [7,] -1.036421328330551894226  1.525817374339748067058  0.47070620500783272311
# [8,] -0.139135286049327872027 -0.065015174557339946992  0.21483758566831215320
# [9,] -0.370005496738202488416  1.573987068922320986530 -1.21431499328084857581
#[10,] -0.070508137614489943545  1.657541962601124518883  0.45886687983031809734