[Rd] rownames() problem with 0-extent arrays (PR#9136)
Prof Brian Ripley
ripley at stats.ox.ac.uk
Wed Aug 16 13:21:16 CEST 2006
R-bugs is not the place to ask questions, see the FAQ ....
On Fri, 11 Aug 2006, richard_raubertas at merck.com wrote:
[...]
> 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):
and that dimension does not have any names, so this is appropriate.
> 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)
Data frames do not have 'dimnames', and what is reported by the accessor
function is list(row.names(x), names(x)), both of which are required to be
character vectors. Indeed, as the help file says
For the \code{"data.frame"} method both dimnames must be non-null, and
the rownames must contain no duplicates nor missing values.
> 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'."
That is accurate.
> 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.
Why would it be desirable to have two equivalent representations of the
same thing (no names)? As you could easily have found out, the C code
replaces a zero-length vector by NULL.
--
Brian D. Ripley, ripley at 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
More information about the R-devel
mailing list