[R] Undocumented behaviour of diag when replacing the diagonal of a matrix?
Duncan Murdoch
murdoch@dunc@n @end|ng |rom gm@||@com
Wed Dec 4 15:31:30 CET 2024
On 2024-12-04 7:38 a.m., Gerrit Eichner wrote:
> Dear list,
>
> is anyone aware of the following behavious of diag when used to replace
> diagonals (plural!) of a matrix?
>
> Small example: The following is documented and clearly to be expected:
>
> A <- matrix(0, nrow = 5, ncol = 5)
> diag(A) <- 1; A
>
>
> BUT, what about the following? When executing the code of `diag<-` line
> by line, it throws errors. So why does it work?
>
> diag(A[-1, ]) <- 2; A
>
> diag(A[-5, -1]) <- 3; A
>
> diag(A[-5, -(1:2)]) <- 4; A
>
Could you show us the log of what you did that generated errors? The
statement `diag(A[-1, ]) <- 2` is pretty complex; it involves two
assignment functions (both `diag<-` and `[<-`), so you might have tried
to execute the wrong thing.
Duncan Murdoch
More information about the R-help
mailing list