diag()

Jonathan Rougier J.C.Rougier@durham.ac.uk
Wed, 18 Aug 1999 09:26:42 +0100 (BST)


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._