Reputation: 1
Trouble in R
I'm having trouble filling a matrix with values using a for() loop. I'm starting with making a blank matrix 1-100
z <- matrix(NA, ncol=100, nrow=100)
q <- 1:100
I need to fill each nrows with 1-100 so that the original matrix becomes the sequence 1-100 in every row
i tried
for(n in 1:nrows(z)){
print(q)
}
but it didn't work as expected.
Upvotes: 0
Views: 2995
Reputation: 37641
print
just prints to the screen without changing your matrix in any way. It would be better to do this without any loop at all.
z <- matrix(rep(1:100, each=100), ncol=100, nrow=100)
If you must use a loop,
for(n in 1:nrow(z)){
z[n,] = q
}
Note that it is nrow
not nrows
Upvotes: 1