diag()

About this list Date view Thread view Subject view Author view Other groups

Subject: diag()
From: Jonathan Rougier (J.C.Rougier@durham.ac.uk)
Date: Wed 18 Aug 1999 - 18:26:42 EST


Message-ID: <Pine.GSO.3.96.990818091901.648B-100000@laplace>

I would like to suggest a slight modification to diag(). In the case
where x is a matrix with both row names and column names the same, it
would be reasonable if the resulting vector also had those names. I often
use diag() on variance matrices, where this modification is helpful.

The modification requires replacing

    if (is.matrix(x) && nargs() == 1)
        return(c(x)[1 + 0:(min(dim(x)) - 1) * (dim(x)[1] + 1)])

the first two lines, with something like

    if (is.matrix(x) && nargs() == 1) {
        y <- c(x)[1 + 0:(min(dim(x)) - 1) * (dim(x)[1] + 1)]
        if (!is.null(nms <- dimnames(x)) && all(nms[[1]]==nms[[2]]))
          names(y) <- nms[[1]]
        return(y)
    }

Just a thought, Jonathan.

Jonathan Rougier Science Laboratories
Department of Mathematical Sciences South Road
University of Durham Durham DH1 3LE

"[B]egin upon the precept ... that the things we see are to be
 weighed in the scale with what we know" (Meredith, 1879, The Egoist)

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


About this list Date view Thread view Subject view Author view Other groups

This archive was generated by hypermail 2b25 : Tue 04 Jan 2000 - 14:16:07 EST