简体   繁体   中英

create a matrix of samples in R

I have a probability distribution X and I would like to create samples of 100 observations: I use sample(X,size=100,replace=TRUE) I would like to plot the sample mean PDF on 100,1000,10000 samples so I tried to create matrices of observations using matrix(sample(X,size=100,replace=TRUE),nrow=100,ncol=100) but it would generate the same sample in all columns. Any ideas on how to create a new sample for each column?

how about this? Substitute rnorm with your sample call. This will take a new sample for each column

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM