Skip to content
Prev 61897 / 63424 Next

NROW and NCOL on NULL

Thank you for your comment,
On Sat, Sep 23, 2023 at 9:51?PM Ben Bolker <bbolker at gmail.com> wrote:
I do not have an answer to this question but it seems to me that code
that relies upon NCOL(NULL) being 1 is not extremely good (and
portable).
Yes, it is documented and somehow clashes with the more intuitive
behaviour of subsetting matrices

 > a <- matrix(1:4,2,2)
[,1] [,2]
[1,]    1    3
[2,]    2    4
[1,]
[2,]
[1] 2 0

NULL is often used to declare an undefined value for the argument of a
function. If such an argument is potentially a matrix, then using NULL
as the default requires additional code to check for the number of
columns and use it in the code.
The same holds to a lesser extent for functions that are expected to
return a matrix and return NULL instead.

Kind regards,

Simone