[Rd] rownames() problem with 0-extent arrays (PR#9136)

richard_raubertas at merck.com richard_raubertas at merck.com
Fri Aug 11 02:35:28 CEST 2006


[R 2.3.1 on Windows XP]

Hello,
The 'do.NULL=FALSE' option of 'rownames' and 'colnames' does not 
work as documented when the array has a 0-extent dimension:

tmp <- matrix(1:15, ncol=3, dimnames=list(letters[1:5], LETTERS[1:3]))
rownames(tmp)  # Fine
rownames(tmp[0,,drop=FALSE])   #  NULL (okay, but see question below)
rownames(tmp[0,,drop=FALSE], do.NULL=FALSE)   #  "row"

The last line returns a character vector of length 1, "row", rather 
than character(0) as implied by the second paragraph of the details 
section of ?rownames:

     "If 'do.NULL' is 'FALSE', a character vector (of length 'NROW(x)'
     or 'NCOL(x)') is returned in any case, prepending 'prefix' to
     simple numbers, if there are no dimnames or the corresponding
     component of the dimnames is 'NULL'."

----------

A separate but related question (not a bug) has to do with the handling
of dimnames for 0-extent arrays.  If an array starts out with non-NULL
dimnames and is subscripted down to have 0-extent, the corresponding 
component is set to NULL, rather than character(0):

dimnames(tmp[0,,drop=FALSE])   # first component is NULL

Why is that?  Note that this is different from how data frames are 
handled:

tmp.df <- data.frame(tmp)
dimnames(tmp.df[0,])  # first component is character(0)

The description in ?dimnames seems to allow character(0) components 
of dimnames, 

     "The dimnames of a matrix or array can be 'NULL' or a list of the
     same length as 'dim(x)'.  If a list, its components are either
     'NULL' or a character vector the length of the appropriate
     dimension of 'x'."

yet the implementation seems to resist them:

tmp2 <- tmp[0,,drop=FALSE]
dimnames(tmp2) <- list(character(0), LETTERS[1:3])
dimnames(tmp2)  #  still NULL

So my question is whether it would be reasonable to change 'dimnames'
and/or "dimnames<-" to use (or at least allow) character(0) rather than 
NULL for array dimensions of 0 extent.

Rich Raubertas
Merck & Co.



More information about the R-devel mailing list