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

Joseph Wood jwood000 @end|ng |rom gm@||@com
Fri Sep 29 18:33:20 CEST 2023


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]]



More information about the R-devel mailing list