Hi Gabor, In strictly reading the help files for both nrow() and row(), the 'x' argument in the former case is "a vector, array, data frame, or NULL.", whereas in the latter case it is "a matrix-like object, that is one with a two-dimensional dim.". Thus, I would expect row() to fail on a >= 3-dimensional array, as your example shows. In reading the help file for slice.index(), there is the following in the See Also section: "row and col for determining row and column indexes; in fact, these are special cases of slice.index corresponding to MARGIN equal to 1 and 2, respectively when x is a matrix." further differentiating the behavior of row() and col() as more specific implementations in the 2-dimensional case. To my read then, the difference in behavior appears to be intentional and expected. Regards, Marc Schwartz ?-----Original Message----- From: R-devel <r-devel-bounces at r-project.org <mailto:r-devel-bounces at r-project.org>> on behalf of Gabor Grothendieck <ggrothendieck at gmail.com <mailto:ggrothendieck at gmail.com>> Date: Sunday, September 8, 2024 at 7:31 AM To: "r-devel at r-project.org <mailto:r-devel at r-project.org>" <r-devel at r-project.org <mailto:r-devel at r-project.org>> Subject: [Rd] Inconsistency between row and nrow In the following nrow provides the expected result but row gives an error. I would have thought that they would both work or both fail. aa <- array(dim = 5:3) nrow(aa) ## [1] 5 row(aa) ## Error in row(aa) : a matrix-like object is required as argument to 'row' # this does work: slice.index(aa, 1) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com ______________________________________________ R-devel at r-project.org <mailto:R-devel at r-project.org> mailing list https://stat.ethz.ch/mailman/listinfo/r-devel <https://stat.ethz.ch/mailman/listinfo/r-devel>
The fact that it is consistent with the documentation is not the point. The point is that the design itself is inconsistent. On Sun, Sep 8, 2024 at 8:27?AM Marc Schwartz <marc_schwartz at me.com> wrote:> > Hi Gabor, > > In strictly reading the help files for both nrow() and row(), the 'x' argument in the former case is "a vector, array, data frame, or NULL.", whereas in the latter case it is "a matrix-like object, that is one with a two-dimensional dim.". > > Thus, I would expect row() to fail on a >= 3-dimensional array, as your example shows. > > In reading the help file for slice.index(), there is the following in the See Also section: > > "row and col for determining row and column indexes; in fact, these are special cases of slice.index corresponding to MARGIN equal to 1 and 2, respectively when x is a matrix." > > further differentiating the behavior of row() and col() as more specific implementations in the 2-dimensional case. > > To my read then, the difference in behavior appears to be intentional and expected. > > Regards, > > Marc Schwartz > > > ?-----Original Message----- > From: R-devel <r-devel-bounces at r-project.org <mailto:r-devel-bounces at r-project.org>> on behalf of Gabor Grothendieck <ggrothendieck at gmail.com <mailto:ggrothendieck at gmail.com>> > Date: Sunday, September 8, 2024 at 7:31 AM > To: "r-devel at r-project.org <mailto:r-devel at r-project.org>" <r-devel at r-project.org <mailto:r-devel at r-project.org>> > Subject: [Rd] Inconsistency between row and nrow > > > In the following nrow provides the expected result but row gives an > error. I would have thought that they would both work or both fail. > > > aa <- array(dim = 5:3) > > > nrow(aa) > ## [1] 5 > > > row(aa) > ## Error in row(aa) : a matrix-like object is required as argument to 'row' > > > # this does work: > slice.index(aa, 1) > > > -- > Statistics & Software Consulting > GKX Group, GKX Associates Inc. > tel: 1-877-GKX-GROUP > email: ggrothendieck at gmail.com > > > ______________________________________________ > R-devel at r-project.org <mailto:R-devel at r-project.org> mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel <https://stat.ethz.ch/mailman/listinfo/r-devel> > > > >-- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
@vi@e@gross m@iii@g oii gm@ii@com
2024-Sep-08 22:36 UTC
[Rd] Inconsistency between row and nrow
It can be informative to look at what the actual functions being discussed do. Dim is an internal, meaning written in some variant of C, perhaps:> dimfunction (x) .Primitive("dim") The function nrow, in my distribution, actually just calls dim() and throws away one dimension:> nrowfunction (x) dim(x)[1L] <bytecode: 0x0000022df4d6f348> <environment: namespace:base> The function row is a bit related in calling dim in one of several ways:> rowfunction (x, as.factor = FALSE) { if (as.factor) { labs <- rownames(x, do.NULL = FALSE, prefix = "") res <- factor(.Internal(row(dim(x))), labels = labs) dim(res) <- dim(x) res } else .Internal(row(dim(x))) } <bytecode: 0x0000022d87fbfb98> <environment: namespace:base> Does this shed any light on why the result may be inconsistent? -----Original Message----- From: R-devel <r-devel-bounces at r-project.org> On Behalf Of Marc Schwartz via R-devel Sent: Sunday, September 8, 2024 8:27 AM To: Gabor Grothendieck <ggrothendieck at gmail.com>; r-devel at r-project.org Subject: Re: [Rd] Inconsistency between row and nrow Hi Gabor, In strictly reading the help files for both nrow() and row(), the 'x' argument in the former case is "a vector, array, data frame, or NULL.", whereas in the latter case it is "a matrix-like object, that is one with a two-dimensional dim.". Thus, I would expect row() to fail on a >= 3-dimensional array, as your example shows. In reading the help file for slice.index(), there is the following in the See Also section: "row and col for determining row and column indexes; in fact, these are special cases of slice.index corresponding to MARGIN equal to 1 and 2, respectively when x is a matrix." further differentiating the behavior of row() and col() as more specific implementations in the 2-dimensional case. To my read then, the difference in behavior appears to be intentional and expected. Regards, Marc Schwartz ?-----Original Message----- From: R-devel <r-devel-bounces at r-project.org <mailto:r-devel-bounces at r-project.org>> on behalf of Gabor Grothendieck <ggrothendieck at gmail.com <mailto:ggrothendieck at gmail.com>> Date: Sunday, September 8, 2024 at 7:31 AM To: "r-devel at r-project.org <mailto:r-devel at r-project.org>" <r-devel at r-project.org <mailto:r-devel at r-project.org>> Subject: [Rd] Inconsistency between row and nrow In the following nrow provides the expected result but row gives an error. I would have thought that they would both work or both fail. aa <- array(dim = 5:3) nrow(aa) ## [1] 5 row(aa) ## Error in row(aa) : a matrix-like object is required as argument to 'row' # this does work: slice.index(aa, 1) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com ______________________________________________ R-devel at r-project.org <mailto:R-devel at r-project.org> mailing list https://stat.ethz.ch/mailman/listinfo/r-devel <https://stat.ethz.ch/mailman/listinfo/r-devel> ______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel