Adeeba Naseem
Adeeba Naseem

Reputation: 1

diag error: nrow or ncol cannot be specified when x is a matrix

Error in diag(nrow(V) * tausq, nrow = ncol(V), ncol = ncol(V)) : 'nrow' or 'ncol' cannot be specified when 'x' is a matrix

This is the error I get when I try to run

D <- diag(nrow(V)*tausq, nrow=ncol(V), ncol=ncol(V))

which is part of a function I wrote.

It's the first line of the function and V is a matrix which is part of the argument.

What does this error mean?

Upvotes: 0

Views: 598

Answers (1)

josliber
josliber

Reputation: 44330

From ?diag, you can read that the function does one of two things -- it either extracts the main diagonal of a passed matrix or it constructs a new diagonal matrix. If you are trying to extract the main diagonal of a matrix (by passing a matrix as the first argument to diag), then you can't pass the nrow and ncol arguments to the diag function.

The error is telling you that apparently nrow(V)*tausq is a matrix in your code -- since nrow(V) is a constant, we conclude that tausq must be a matrix. As a result, it's giving you an error due to setting nrow and ncol.

Long story short -- you seem to be assuming nrow(V)*tausq is a constant, but in fact it's a matrix. You need to convert tausq to a constant to proceed as you want to.

Upvotes: 1

Related Questions