Martin Maechler
2012-Feb-04 16:38 UTC
[Rd] RFC: Proposal to make NROW() and NCOL() slightly more general
The help has> Description:> 'nrow' and 'ncol' return the number of rows or columns present in 'x'. > 'NCOL' and 'NROW' do the same treating a vector as 1-column matrix.and> x: a vector, array or data frameI'm proposing to extend these two convenience functions to also work ``correctly'' for generalized versions of matrices. The current implementation : NROW <- function(x) if(is.array(x)||is.data.frame(x)) nrow(x) else length(x) NCOL <- function(x) if(is.array(x) && length(dim(x)) > 1L || is.data.frame(x)) ncol(x) else 1L only treats something as matrix when is.array(.) is true, which is not the case, e.g., for multiprecision matrices from package 'gmp' or for matrices from packages SparseM, Matrix or similar. Of course, all these packages could write methods for NROW, NCOL for their specific matrix class, but given that the current definition is so simple, I'd find it an unnecessary complication. Rather I propose the following new version NROW <- function(x) if(length(dim(x)) || is.data.frame(x)) nrow(x) else length(x) NCOL <- function(x) if(length(dim(x)) > 1L || is.data.frame(x)) ncol(x) else 1L I've tested to see that it does not change any of the R 'make check-all' checks... but I'd really like to let this pass by as a general RFC.. (in spite of the fact that I'll offline for almost all the rest of the weekend). Martin Maechler
Hadley Wickham
2012-Feb-04 17:08 UTC
[Rd] RFC: Proposal to make NROW() and NCOL() slightly more general
On Sat, Feb 4, 2012 at 10:38 AM, Martin Maechler <maechler at stat.math.ethz.ch> wrote:> The help has > >> Description: > >> ? 'nrow' and 'ncol' return the number of rows or columns present in 'x'. >> ? 'NCOL' and 'NROW' do the same treating a vector as 1-column matrix. > > and > >> ? x: a vector, array or data frame > > I'm proposing to extend these two convenience functions > to also work ``correctly'' for generalized versions of matrices. > > > The current implementation : > > NROW <- function(x) if(is.array(x)||is.data.frame(x)) nrow(x) else length(x) > NCOL <- function(x) if(is.array(x) && length(dim(x)) > 1L || is.data.frame(x)) ncol(x) else 1L > > only treats something as matrix when ?is.array(.) is true, > which is not the case, e.g., for multiprecision matrices from > package 'gmp' or for matrices from packages SparseM, Matrix or similar. > > Of course, all these packages could write methods for NROW, NCOL > for their specific matrix class, but given that the current > definition is so simple, > I'd find it an unnecessary complication. > > Rather I propose the following new version > > NROW <- function(x) if(length(dim(x)) || is.data.frame(x)) nrow(x) else length(x) > NCOL <- function(x) if(length(dim(x)) > 1L || is.data.frame(x)) ncol(x) else 1LThat makes me wonder about: DIM <- function(x) if (length(dim(x)) > 1L) dim(x) else c(length(x), 1L) or maybe more efficiently: DIM <- function(x) { d <- dim(x) if (length(d) > 1L) dim(x) else c(length(x), 1L) } given that dim() is not always trivial to compute (e.g. for data frames it can be rather slow if you're doing it for hundreds of data frames) then NROW and NCOL could be exact equivalents to nrow and ncol. Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/