The question is about R syntax, as BruceET says, but I think that data simulation is an important topic, and that you could (should?) generate your data in another way. You can use the mvtnorm
package to generate random multivariate matrices and the scale()
function to ensure that each column has 0 meanmean=0 and 1 variancevariance=1:
> library(mvtnorm)
> n <- 6
> p <- 3
> mean <- rep(0, p)
> sigma <- diag(p) # identity matrix
> X <- rmvnorm(n, mean=mean, sigma=sigma)
> X
[,1] [,2] [,3]
[1,] -0.75184545 4557803 0-1.1306994 6174866 0.88685086134224496
[2,] 0.081937197982673 -10.48027991214584 -03.00127638505775038
[3,] -0.787214212175150 -2 1.63284011449116 -0.94581572407709425
[4,] 0.218120929070137 0.62484874484822 -01.22889928249516553
[5,] 0.12225648 -1.14319248435512 -2.4068282 1.75790172323142658
[6,] -0.260354075840670 -01.50810483760948 -0.78028384437929271
> meanapply(X[X,1] MARGIN=2, FUN=mean)
[1] -0.032888212326054 -0.6547457 1.0971624
> sdapply(X[X,1] MARGIN=2, FUN=sd)
[1] 01.5129346012930 1.360682 1.108703
> X <- scale(X)
> meanapply(X[X,1] MARGIN=2, FUN=mean)
[1] 6 1.938894e127570e-1817 -3.699840e-17 9.483155e-17
> sdapply(X[X,1] MARGIN=2, FUN=sd)
[1] 1 1 1
> X <- scale(rmvnorm(1000, mean=mean, sigma=sigma))
> cor(X[X)
[,1],X[ [,2]) [,3]
[1][1,] 1.00000000 -0.0337255105327000 -0.01848098
[2,] -0.05327000 1.00000000 -0.01011558
[3,] -0.01848098 -0.01011558 1.00000000
> X <- scale(rmvnorm(10000, mean=mean, sigma=sigma))
> cor(X[X)
[,1],X[ [,2]) [,3]
[1][1,] -1.000000000 0.008293499005957725 0.002865598
[2,] 0.005957725 1.000000000 0.008932789
[3,] 0.002865598 0.008932789 1.000000000