[Rd] collapsing list of NULLs dimnames into a NULL
Hervé Pagès
hpages at fredhutch.org
Fri Jun 10 22:26:09 CEST 2016
Hi,
Here are two different ways to create a 0 x 0 logical matrix:
m1 <- matrix(nrow=0, ncol=0)
m1
# <0 x 0 matrix>
m2 <- as.matrix(data.frame())
m2
# <0 x 0 matrix>
Surprisingly:
identical(m1, m2)
# [1] FALSE
That's because of their dimnames:
dimnames(m1)
# NULL
dimnames(m2)
# [[1]]
# NULL
#
# [[2]]
# NULL
More generally this can be observed with 0-col matrices:
m1 <- matrix(nrow=5, ncol=0)
m2 <- as.matrix(data.frame(stuff=11:15)[0])
identical(m1, m2) # FALSE
The culprit is this line in as.matrix.data.frame():
dimnames(X) <- list(dn[[1L]], unlist(collabs, use.names = FALSE))
Shouldn't as.matrix.data.frame() collapse the list of NULLs into a NULL
before setting the dimnames?
Alternatively, maybe a more general solution could be to have the
dimnames() setter take care of this. There seems to be a lot of
code around that is potentially setting a list of NULLs as the dimnames
of an object, so having dimnames<- do the collapse would fix the problem
in one place once for all and avoid a lot of repeated code.
Finally, having this functionality available at the C level would be
great. Would basically be a wrapper to
Rf_setAttrib(x, R_DimNamesSymbol, dimnames)
that takes care of the collapse. The SET_DIMNAMES macro could be
redefined to call that wrapper instead...
Thanks,
H.
--
Hervé Pagès
Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024
E-mail: hpages at fredhutch.org
Phone: (206) 667-5791
Fax: (206) 667-1319
More information about the R-devel
mailing list