Re: [Rd] NROW and NCOL on NULL
I just saw this r85704 | hornik | 2023-12-19 00:33:07 -0600 (Tue, 19 Dec 2023) | 1 line Changed paths: M /trunk/doc/NEWS.Rd M /trunk/src/library/base/R/matrix.R M /trunk/src/library/base/man/nrow.Rd M /trunk/src/library/profile/Common.R Have NCOL(NULL) return 0 instead of 1. Many thanks to Kurt and the whole R-core team! Simone On Sat, Sep 23, 2023 at 7:43 PM 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 > > -- > ___ > > Simone Giannerini > Dipartimento di Scienze Statistiche "Paolo Fortunati" > Universita' di Bologna > Via delle belle arti 41 - 40126 Bologna, ITALY > Tel: +39 051 2098262 Fax: +39 051 232153 > https://simonegiannerini.net/ > ___ -- ___ Simone Giannerini Dipartimento di Scienze Statistiche "Paolo Fortunati" Universita' di Bologna Via delle belle arti 41 - 40126 Bologna, ITALY Tel: +39 051 2098262 Fax: +39 051 232153 https://simonegiannerini.net/ __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] NROW and NCOL on NULL
> Simone Giannerini > on Sun, 24 Sep 2023 16:57:00 +0200 writes: > Thank you for your comment, On Sat, Sep 23, 2023 at > 9:51 PM Ben Bolker wrote: >> >> This is certainly worth discussing, but there's always a >> heavy burden of back-compatibility; how much better would >> it be for NCOL and NROW to both return zero, vs. the >> amount of old code that would be broken? > 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). Well, it remains *very* portable, as long as we keep the behavior. It has worked as it does for more than twenty years, and if you finally remain convinced that we won't change, it will remain portable between all versions of R from the very old past to the remote future ... ;-) Martin >> Furthermore, the reason for this behaviour is justified >> as consistency with the behaviour of as.matrix() and >> cbind() for zero-length vectors, from ?NCOL: >> >> ## as.matrix() produces 1-column matrices from 0-length >> vectors, ## and so does cbind() : >> >> (of course you could argue that this behaviour should be >> changed as well ...) >> >> > Yes, it is documented and somehow clashes with the more > intuitive behaviour of subsetting matrices >> a <- matrix(1:4,2,2) a > [,1] [,2] [1,] 1 3 [2,] 2 4 >> a2 <- a[,-(1:2)] a2 > [1,] [2,] >> dim(a2) > [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 >> On 2023-09-23 3:41 p.m., Simone Giannerini wrote: > 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 >> 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 >> >>> >> >> >> > >> > >> >> __ >> R-devel@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel > -- > ___ > Simone Giannerini Dipartimento di Scienze Statistiche > "Paolo Fortunati" Universita' di Bologna Via delle belle > arti 41 - 40126 Bologna, ITALY Tel: +39 051 2098262 Fax: > +39 051 232153 https://simonegiannerini.net/ > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] NROW and NCOL on NULL
Thank you for your comment, On Sat, Sep 23, 2023 at 9:51 PM Ben Bolker wrote: > > This is certainly worth discussing, but there's always a heavy > burden of back-compatibility; how much better would it be for NCOL and > NROW to both return zero, vs. the amount of old code that would be broken? 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). > >Furthermore, the reason for this behaviour is justified as > consistency with the behaviour of as.matrix() and cbind() for > zero-length vectors, from ?NCOL: > > ## as.matrix() produces 1-column matrices from 0-length vectors, > ## and so does cbind() : > > (of course you could argue that this behaviour should be changed as > well ...) > > Yes, it is documented and somehow clashes with the more intuitive behaviour of subsetting matrices > a <- matrix(1:4,2,2) > a [,1] [,2] [1,]13 [2,]24 > a2 <- a[,-(1:2)] > a2 [1,] [2,] > dim(a2) [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 > On 2023-09-23 3:41 p.m., Simone Giannerini wrote: > > 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 > > 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 > >>> > >> > > > > > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- ___ Simone Giannerini Dipartimento di Scienze Statistiche "Paolo Fortunati" Universita' di Bologna Via delle belle arti 41 - 40126 Bologna, ITALY Tel: +39 051 2098262 Fax: +39 051 232153 https://simonegiannerini.net/ __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] NROW and NCOL on NULL
On 23/09/2023 3:41 p.m., Simone Giannerini wrote: 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. If you don't list any examples of problems, then the only possible conclusion is that there aren't any except obscure ones, so the answer is clearly that it is not worth it to make this change. Duncan Murdoch Regards, Simone On Sat, Sep 23, 2023 at 7:50 PM Duncan Murdoch 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 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] NROW and NCOL on NULL
À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 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 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] NROW and NCOL on NULL
This is certainly worth discussing, but there's always a heavy burden of back-compatibility; how much better would it be for NCOL and NROW to both return zero, vs. the amount of old code that would be broken? Furthermore, the reason for this behaviour is justified as consistency with the behaviour of as.matrix() and cbind() for zero-length vectors, from ?NCOL: ## as.matrix() produces 1-column matrices from 0-length vectors, ## and so does cbind() : (of course you could argue that this behaviour should be changed as well ...) On 2023-09-23 3:41 p.m., Simone Giannerini wrote: 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 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 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] NROW and NCOL on NULL
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 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 > > > -- ___ Simone Giannerini Dipartimento di Scienze Statistiche "Paolo Fortunati" Universita' di Bologna Via delle belle arti 41 - 40126 Bologna, ITALY Tel: +39 051 2098262 Fax: +39 051 232153 https://simonegiannerini.net/ __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] NROW and NCOL on NULL
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 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] NROW and NCOL on NULL
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 -- ___ Simone Giannerini Dipartimento di Scienze Statistiche "Paolo Fortunati" Universita' di Bologna Via delle belle arti 41 - 40126 Bologna, ITALY Tel: +39 051 2098262 Fax: +39 051 232153 https://simonegiannerini.net/ __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel