The problem is how R fills the columns matrix. Here is a simple example that illustrates this:
mat1 <- matrix(1:9, ncol = 3) mat2 <- matrix(1:9, ncol = 3) mat2[-1, -1] <- mat1[1, -1] mat2 > mat2 [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 4 4 [3,] 3 7 7
mat1[1, -1] is a 4,7 vector, which you can see that R used to fill the bits of the mat2 column by columns. You need an operation with multiple lines.
One solution is to replicate the replacement vector as many times as needed:
> mat2[-1, -1] <- rep(mat1[1, -1], each = nrow(mat1)-1) > mat2 [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 4 7 [3,] 3 4 7
This works because calling rep() replicates every value in the vector when we use the argument "each" instead of replicating (repeating) the vector:
> rep(mat1[1, -1], each = nrow(mat1)-1) [1] 4 4 7 7
The default behavior will also give the wrong answer:
> rep(mat1[1, -1], nrow(mat1)-1) [1] 4 7 4 7
In particular, the problem you see is also the way that R expands the arguments to an appropriate length for replacement. R actually and silently extends the replacement vector exactly as rep(mat1[1, -1], nrow(mat1)-1) , which, in combination with the column fill principle, gives the behavior you saw.