NCOL, as.matrix, cbind and NULL

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

NCOL, as.matrix, cbind and NULL

Gabriel Becker-2
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

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)

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


[1] 1

> cbind(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?



        [[alternative HTML version deleted]]

[hidden email] mailing list