Skip to content

"diag", "diag<-" and "[" , "[<-"

2 messages · bergarog at gmail.com, Duncan Murdoch

#
On 07/01/2010 9:31 AM, bergarog at gmail.com wrote:
The problem isn't with diag, it's with "[<-".  When you include drop=F, 
it doesn't appear to handle properly the matrix indexing that diag() uses.

So a simpler example is just

M[1,1, drop=FALSE] <- 1

which generates the same error you saw.  I'd call this a bug; I'll look 
into it.  ("[<-" is done internally in some fairly ugly code, so this 
might take a while.)

As a workaround, I think this does what you want:

n <- 1
submatrix <- M[1:n,1:n,drop=FALSE]
diag(submatrix) <- rep(100, n)
M[1:n,1:n] <- submatrix

Duncan Murdoch