[Rd] Use of `[` with array and resulting class

Matt Denwood md @end|ng |rom @und@ku@dk
Sat Sep 30 09:40:06 CEST 2023


Hi Joseph

This behaviour is as expected (and as documented by ?`[`) because the default of drop=TRUE coerces the result to the lowest possible dimension, which in your case is a vector rather than array.  Using for example:

b[1,,,drop=FALSE]

... results in an output array with the same number of dimensions (i.e. length of dim()) as the input array.  I always use drop=FALSE when programming in situations where the dimensions of the input array aren't known (and forgetting to do so has several times bitten me when I later come to pass in an array like yours).

I am not aware of a way to make the extract operator behave as you apparently expected (drop the dimension corresponding to the supplied length-1 index but leaving all others even if they are also length 1).  If you want this behaviour then you would be best to create a wrapper function, or perhaps even a derived class, but that is not a topic for r-devel (rather r-help).

Best wishes,

Matt



On 30/09/2023, 05:28, "R-devel on behalf of Joseph Wood" <r-devel-bounces using r-project.org <mailto:r-devel-bounces using r-project.org> on behalf of jwood000 using gmail.com <mailto:jwood000 using gmail.com>> wrote:


[You don't often get email from jwood000 using gmail.com <mailto:jwood000 using gmail.com>. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification <https://aka.ms/LearnAboutSenderIdentification> ]


Hello,


I recently discovered a possible inconsistency with usage of an object of
class array.


Consider the following example:


## Setup


a <- array(1:6, dim = c(1, 3, 2))
a
, , 1


[,1] [,2] [,3]
[1,] 1 2 3


, , 2


[,1] [,2] [,3]
[1,] 4 5 6


class(a)
[1] "array"


dim(a)
[1] 1 3 2


## Now use `[`
a[1,,]
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6


class(a[1,,])
[1] "matrix" "array"


dim(a[1,,])
[1] 3 2


Up until this point, it makes sense to me. Now, let's consider when dim =
c(1, 6, 1). This is where I have a little trouble understanding the
behavior.


## Array with dim = c(1, any_number_here, 1)


b <- array(1:6, dim = c(1, 6, 1))
b
, , 1


[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 2 3 4 5 6


class(b)
[1] "array"


dim(b)
[1] 1 6 1


## The problem


b[1,,]
[1] 1 2 3 4 5 6


dim(b[1,,])
NULL


class(b[1,,])
[1] "integer"


I would have expected:


b[1,,] ## produced the output with matrix(1:6, ncol = 1)
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6


class(b[1,,])
[1] "matrix" "array"


dim(b[1,,])
[1] 3 1


Is this a bug? If not, any help understanding this behaviour would be much
appreciated.


Thanks,
Joseph


[[alternative HTML version deleted]]


______________________________________________
R-devel using r-project.org <mailto:R-devel using r-project.org> mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel <https://stat.ethz.ch/mailman/listinfo/r-devel>





More information about the R-devel mailing list