From ru|pb@rr@d@@ @end|ng |rom @@po@pt  Sat Sep 23 23:38:29 2023
From: ru|pb@rr@d@@ @end|ng |rom @@po@pt (Rui Barradas)
Date: Sat, 23 Sep 2023 22:38:29 +0100
Subject: [Rd] NROW and NCOL on NULL
In-Reply-To: <CANcXGiz9g6mAsnNd3=Uc9iQ5xvE8Mg2ws5fbsEFYVXB_HL=Sog@mail.gmail.com>
References: <CANcXGizv3sD3kiE__EUkuaESn44mvtQMrtpO_k=FjUwFQDGnVQ@mail.gmail.com>
 <936870db-2e16-4b8b-878b-c2dde317319c@gmail.com>
 <CANcXGiz9g6mAsnNd3=Uc9iQ5xvE8Mg2ws5fbsEFYVXB_HL=Sog@mail.gmail.com>
Message-ID: <b5869e21-9b54-4500-9c18-7bd64395aa57@sapo.pt>

?s 20:41 de 23/09/2023, Simone Giannerini escreveu:
> I know it's documented and I know there are other ways to guard
> against this behaviour, once you know about this.
> The point is whether it might be worth it to make NCOL and NROW return
> the same value on NULL and make R more consistent/intuitive and
> possibly less error prone.
> 
> Regards,
> 
> Simone
> 
> On Sat, Sep 23, 2023 at 7:50?PM Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
>>
>> It's been documented for a long time that NCOL(NULL) is 1.  What
>> particular problems did you have in mind?  There might be other ways to
>> guard against them.
>>
>> Duncan Murdoch
>>
>> On 23/09/2023 1:43 p.m., Simone Giannerini wrote:
>>> Dear list,
>>>
>>> I do not know what would be the 'correct' answer to the following but
>>> I think that they should return the same value to avoid potential
>>> problems and hard to debug errors.
>>>
>>> Regards,
>>>
>>> Simone
>>> ---------------------------------------
>>>
>>>> NCOL(NULL)
>>> [1] 1
>>>
>>>> NROW(NULL)
>>> [1] 0
>>>
>>>> sessionInfo()
>>> R version 4.3.1 RC (2023-06-08 r84523 ucrt)
>>> Platform: x86_64-w64-mingw32/x64 (64-bit)
>>> Running under: Windows 11 x64 (build 22621)
>>>
>>> Matrix products: default
>>>
>>>
>>> locale:
>>> [1] LC_COLLATE=Italian_Italy.utf8  LC_CTYPE=Italian_Italy.utf8
>>> [3] LC_MONETARY=Italian_Italy.utf8 LC_NUMERIC=C
>>> [5] LC_TIME=Italian_Italy.utf8
>>>
>>> time zone: Europe/Rome
>>> tzcode source: internal
>>>
>>> attached base packages:
>>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>>
>>> loaded via a namespace (and not attached):
>>> [1] compiler_4.3.1
>>>
>>
> 
> 
Hello,

The way I think of this behavior that made it intuitive is to think that 
in R matrices are column-major, therefore if length(NULL) == 0 then NULL 
can be seen as a matrix with one column and zero rows, no data.

Here are other examples for which that reasoning works:


m <- matrix(integer(0L))
NCOL(m)
NROW(m)

x <- integer(0L)
NCOL(x)
NROW(x)


Not very convincing? Maybe with time...

Hope this helps,

Rui Barradas



[Rd] NROW and NCOL on NULL
Rui Barradas