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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>>>>> On Wed, 18 Aug 1999 09:26:42 +0100 (BST), Jonathan Rougier <J.C.Rougier@durham.ac.uk> said:JonR> I would like to suggest a slight modification to diag(). In the JonR> case where x is a matrix with both row names and column names the JonR> same, it would be reasonable if the resulting vector also had JonR> those names. I often use diag() on variance matrices, where this JonR> modification is helpful. JonR> The modification requires replacing JonR> if (is.matrix(x) && nargs() == 1) JonR> return(c(x)[1 + 0:(min(dim(x)) - 1) * (dim(x)[1] + 1)]) JonR> the first two lines, with something like JonR> if (is.matrix(x) && nargs() == 1) { JonR> y <- c(x)[1 + 0:(min(dim(x)) - 1) * (dim(x)[1] + 1)] JonR> if (!is.null(nms <- dimnames(x)) && all(nms[[1]]==nms[[2]])) JonR> names(y) <- nms[[1]] JonR> return(y) JonR> } JonR> Just a thought, Jonathan. JonR> Jonathan Rougier Science Laboratories JonR> Department of Mathematical Sciences South Road JonR> University of Durham Durham DH1 3LE This seems like a good idea [ and I think I vaguely remember that I would have wanted that myself, some time], however, R 0.65 is in ``feature freeze'' now (and release date is scheduled for Aug.27 ..) Yes, please ``everyone'' : Now is pre-tester time, Get "R-devel.tar.gz" from a CRAN site near you, unpack and install {from source!!}, and send bug reports to R-core or R-devel, *not* to R-bugs -- since bugs in non-released versions shouldn't make it into the R repository I think. {I hope to not forget to put the above diag() improvement into the next release after 0.65(.0)..} 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._