Hi all,

so I've known for a while that NROW(NULL) gives 0, where nrow(NULL) gives
an error, so I naively expected NCOL to do the same.

Of course, it does not, and is documented* (more on this in a bit) as not
doing so. For those reading without the documentation open, it gives 1.

The relevant doc states:

‘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, even a
0-length vector, compatibly with ‘as.matrix()’ or ‘cbind()’, see the
example.

But there are a couple of fiddly bits here. First is that it says "even a
0-length *vector*" (emphasis mine), but we have

> is.vector(NULL)
[1] FALSE

As opposed, of course, to, e.g., numeric(0).

Next is the claim of compatibility with as.matrix and cbind, but in both my
released version of R (4.0.2) and devel that I just built from trunk, we
have

> NCOL(NULL)

[1] 1

> cbind(NULL)

NULL

> as.matrix(NULL)

*Error in array(x, c(length(x), 1L), if (!is.null(names(x)))
list(names(x),  : *

*  'data' must be of a vector type, was 'NULL'*


So in fact each function is treating NULL completely differently.


The fix (to change behavior or to add a mention in the documentation that
NULL is treated as a 0-length vector) would be easy to do, should I file a
bug with a patch for this?


Best,

~G

        [[alternative HTML version deleted]]

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to