Reputation: 23
I have one question about putting a simulated data in a matrix format, but I cannot suitably write its program in R, and constantly receive an error, I guess my "rep" definition and final "Matrix" expression are somehow wrong, but I do not know how to fix them. Here my specific question is:
I would like to produce a matrix contains generated values. I have 20000 generated values for x and y. As the output, I like to have a (2000 by 10) matrix that each column of the matrix contains the output of following for loop.
My R.code:
x=rnorm(2e4,5,6)
vofdiv=quantile(x,probs=seq(0,1,0.1))
y=rnorm(2e4,4,6)
Matrix=rep(NULL,2000)
for(i in 1:10)
{
Matrix[i]=y[(x>=vofdiv[i] & x<vofdiv[i+1])] #The i(th) col of matrix
}
Matrix # A 2000*10 Matrix, as the final output
I highly appreciate that someone helps me!
Upvotes: 1
Views: 70
Reputation: 92292
You have several problems here.
First of all, the correct way to define an empty matrix of size 2e4*10
, would be
Matrix <- matrix(NA, 2e4, 10)
Although you could potentially create a matrix using your way(rep
) and then use dim
, something like
Matrix <- rep(NA, 2e5)
dim(Matrix) <- c(2e4, 10)
Second problem is, when trying to insert into a column in a matrix
, you need to index it correctly, i.e.,
Matrix[, i] <-
instead of
Matrix[i] <-
The latter will index Matrix
as if it was a vector (which is it basically is). In other words, it will convert a 2000*10 matrix
to a 20000 length single vector
and index it.
The third problem is, that when your loop reaches i = 11
and you are running x<vofdiv[i+1]
you are always excluding the last values which are x == vofdiv[11]
, thus you are always getting less than 2000 values:
for(i in 1:10)
{
print(length(y[ (x >= vofdiv[i] & x < vofdiv[i+1])]))
}
# [1] 2000
# [1] 2000
# [1] 2000
# [1] 2000
# [1] 2000
# [1] 2000
# [1] 2000
# [1] 2000
# [1] 2000
# [1] 1999 <----
Thus, it will give you an error if you will try to replace 2000 length vector with 1999 length one, because a matrix
in R can't contain different dimensions for each column.
The workaround would be to add =
to your last statement, such as
Matrix <- matrix(NA, 2e4, 10)
for(i in 1:10)
{
Matrix[, i] <- y[x >= vofdiv[i] & x <= vofdiv[i + 1]]
}
Upvotes: 3