[R] Undocumented behaviour of diag when replacing the diagonal of a matrix?

Gerrit Eichner gerr|t@e|chner @end|ng |rom m@th@un|-g|e@@en@de
Wed Dec 4 13:38:45 CET 2024


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


Any ideas?

  TIA and best regards  --  Gerrit

---------------------------------------------------------------------
Dr. Gerrit Eichner                   Mathematical Institute, Room 215
gerrit.eichner using math.uni-giessen.de   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
https://www.uni-giessen.de/math/eichner



More information about the R-help mailing list