On Sun, 17 May 2009, goodr...@fas.harvard.edu wrote:
Full_Name: Ben Goodrich
Version: 2.9.0
OS: Linux (Debian unstable)
Submission from: (NULL) (128.103.220.16)
row(x), col(x), and functions that call them like lower.tri(x) and
upper.tri(x) do not retain the rownames or colnames of x in the
matrix that is returned. Example from R version 2.9.0 :
x <- matrix(1:9, nrow = 3, ncol = 3)
rownames(x) <- LETTERS[1:3]
colnames(x) <- letters[1:3]
dimnames(row(x)) # NULL
dimnames(col(x)) # NULL
Is there anyone for whom the expected behavior is to drop the
dimnames of x ? It is not consistent with other functions of
matrices.
I suspect everyone who reads the help page carefully.
This is not a 'function of a matrix' x but of dim(x) for a
'matrix-like' object. The help page makes this clear to me (I am not
the author), so I think you have misread it. There is no question of
'drop the dimames' nor 'retaining the rownames': it is a new object,
an integer matrix whatever x was.
These functions are mainly for use in programming, and need to be
efficient -- so adding dimnames that will never be needed by all the
existing code is a non-trivial overhead.
By default, row(x) already
returns the row numbers of x (and similarly for col()), so how would seeing
integers in the rownames be helpful?
Without patch:
row(x)
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 2 2 2
[3,] 3 3 3
With patch:
row(x)
a b c
A 1 1 1
B 2 2 2
C 3 3 3
Patch:
Index: src/library/base/R/matrix.R
===================================================================
--- src/library/base/R/matrix.R (revision 48553)
+++ src/library/base/R/matrix.R (working copy)
@@ -104,8 +104,9 @@
labs <- rownames(x, do.NULL=FALSE, prefix="")
res <- factor(.Internal(row(dim(x))), labels=labs)
dim(res) <- dim(x)
- res
- } else .Internal(row(dim(x)))
+ } else res <- .Internal(row(dim(x)))
+ dimnames(res) <- dimnames(x)
+ res
}
Lots of issues here: dimnames() is generic and may not be compatible
with the object produced, so checks are needed. And for efficiency
this should be done in the C-level code that creates the object if
done at all.
col <- function(x, as.factor=FALSE)
@@ -114,8 +115,9 @@
labs <- colnames(x, do.NULL=FALSE, prefix="")
res <- factor(.Internal(col(dim(x))), labels=labs)
dim(res) <- dim(x)
- res
- } else .Internal(col(dim(x)))
+ } else res <- .Internal(col(dim(x)))
+ dimnames(res) <- dimnames(x)
+ res
}
crossprod <- function(x, y=NULL) .Internal(crossprod(x,y))
______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
--
Brian D. Ripley, rip...@stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel