Skip to content

diag()

4 messages · Jonathan Rougier, Peter Dalgaard, Martin Maechler

#
Hi Martin,

Just to follow my previous suggestion to modify diag(), the code I
suggested is vulnerable to matrices with dimnames(x) <- list(NULL, NULL),
which does occasionally happen (it has just happened to me!), so the
following would be better ...

"diag" <-
function (x = 1, nrow, ncol = n) 
{
    if (is.matrix(x) && nargs() == 1) {
        md <- min(dim(x))
        y <- c(x)[1 + 0:(md - 1) * (dim(x)[1] + 1)]
        nms <- dimnames(x)
        if (is.list(nms) && !any(sapply(nms, is.null)) &&
          all(nms[[1]][1:md] == nms[[2]][1:md])) 
            names(y) <- nms[[1]][1:md]
        return(y)
    }

... the rest as in the original function.  Cheers, 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
JonR> Just to follow my previous suggestion to modify diag(), the code
    JonR> I suggested is vulnerable to matrices with dimnames(x) <-
    JonR> list(NULL, NULL), which does occasionally happen (it has just
    JonR> happened to me!), so the following would be better ...

    JonR> "diag" <-
    JonR> function (x = 1, nrow, ncol = n) 
    JonR> {
    JonR>   if (is.matrix(x) && nargs() == 1) {
    JonR>     md <- min(dim(x))
    JonR>     y <- c(x)[1 + 0:(md - 1) * (dim(x)[1] + 1)]
    JonR>     nms <- dimnames(x)
    JonR>     if (is.list(nms) && !any(sapply(nms, is.null)) &&
    JonR>         all(nms[[1]][1:md] == nms[[2]][1:md])) 
    JonR>         names(y) <- nms[[1]][1:md]
    JonR>     return(y)
    JonR>   }

    JonR> ... the rest as in the original function.  Cheers, Jonathan.

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

Your proposal has been (slightly improved) and commited for 0.64.1 
(or tomorrow's snapshot of R-release).

NEWS has
     
    o	diag(x) now keeps names when appropriate and barfs when x is an array.


Thank you very much.	Martin



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Martin Maechler <maechler@stat.math.ethz.ch> writes:
^
Um...
- unless it's of dimension 2, you mean. But is this really desirable
for the 1d case? diag(array(1:9)) used to work like diag(1:9), but is
now an error. E.g. the output of table() can be a 1d array.
2 days later
#
PD> Martin Maechler <maechler@stat.math.ethz.ch> writes:
    >> Your proposal has been (slightly improved) and commited for 0.64.1
    PD>                                                                  ^
    PD> Um...

    >> NEWS has
    >> 
    >> o diag(x) now keeps names when appropriate and barfs when x is an
    >> array.

    PD> - unless it's of dimension 2, you mean.
yes..
    PD> But is this really
    PD> desirable for the 1d case? diag(array(1:9)) used to work like
    PD> diag(1:9), but is now an error. E.g. the output of table() can be a
    PD> 1d array.

I know.  
But I think that people may expect something else from diag(<array>).
I don't mind letting the 1D case still go by, but certainly not d >= 3 !

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