Angelina
Angelina

Reputation: 71

Loop with matrix in R

Suppose,I have a matrix of 10 X 100 as follow (actually matrix quite big):

x = matrix(norm(n=10), 10, 100)

Now I want to make 1000 by 1 matrix by combining column 1 though 100. [for example like rbind.

my code is as follow:

y=matrix(0,nrow=1000,ncol=1) 

for(row in 1:10){
y[1+(row-1)*100:(row)*100, 1]=x[row,1:100] 
}

My expected output is something like this

y=matrix(rnorm(1000),nrow=1000, ncol=1)

Somehow my code does not seem to be correct.

Upvotes: 0

Views: 81

Answers (1)

dcarlson
dcarlson

Reputation: 11076

All you need to do is change the dimensions of the matrix:

x <- matrix(1:100, 5, 20)
y <- matrix(x, 100, 1)

Upvotes: 1

Related Questions