[R] diagonal matrix construction

Gabor Grothendieck
Wed, 13 Oct 2004 03:22:31 -0000

Chris Ryan <chrisryan <at> wvdnr.gov> writes:

:
: Hi,
:
: I have worked long and hard and looked in the manuals and am having a hard
time constructing a diagonal
: matrix.  I can get the diagonals out of a matrix but can't construct the
matrix with just the diagonals.  I
: have been on the web site and manuals and I think that it says to use:
:
: dsj <- diag (three = 1, nrow, ncol = 7)                           three is
the name of my matrix and dsj is where I'm trying to put it but it
: comes back:
:
: unused argument(s) (three ...)
: every time that I try it.
:
: Am I doing something wrong in trying to make this matrix?
:
: Thank you very much for your time, Chris Ryan
:

Looking at ?diag we see that diag takes 3 arguments: 'x', 'nrow'
and 'ncol'.
'three' is not one of them so it is correctly telling you that you
specified
an unused argument.

diag(nrow = 7) # will create a 7x7 identity matrix.
diag(1, 7)  # same using positional rather than named arguments
diag(, 7)  # same since x defaults to 1

Look at the examples at the bottom of the ?diag page and try them out
by running

example(diag)

at the R prompt or by just typing them in to get more insight.  Also
you might consider rereading the Introduction to R manual where it
is covered on page 22.