I’ve got a matrix (mat1), say 100 rows and 100 columns; I want to create another matrix where every row is the same as the 1st row in mat1 (except that I want to keep the 1st col as the original values)
I’ve managed to do this using a loop:
mat2 <- mat1
for(i in 1:nrow(mat1))
{
mat2[i,2:ncol(mat2)] <- mat1[1,2:ncol(mat1)]
}
this works and produces the result I expect; however, I’d have thought there should be a way to do it without a loop; I’ve tried:
mat2 <- mat1
mat2[c(2:100),2:ncol(mat2)] <- mat1[1,2:ncol(mat1)]
Can someone point out my error?!
Thanks,
Chris
The problem is the way R fills matrices, by columns. Here is a simple example that illustrates this:
mat1[1, -1]is the vector4,7, which you can see that R has used to fill the bit ofmat2column-wise. You wanted a row-wise operation.One solution is to replicate the replacement vector as many times as is required:
This works because the
rep()call replicates each value in the vector when we use the"each"argument, instead of replicating (repeating) the vector:The default behaviour would also give the wrong answer:
In part, the problem you are seeing is also the way R extends arguments to the appropriate length for the replacement. R actually, and silently, extended the replacement vector exactly in the way
rep(mat1[1, -1], nrow(mat1)-1)does, which when coupled with the fill-by-column principle gave the behaviour you saw.