This is due to `[` dropping dimensions by default. In your first example, think of a[1, , ] as having dimension c(1, 3, 2), but, because drop = TRUE, all dimensions of extent 1 (the first dimension) are dropped and the result has dimension c(3, 2). In your second example, b[1, , ] would have dimension c(1, 6, 1), but now both the first and third dimensions are dropped, resulting in a vector with no dimensions.
We can use the drop function to explicitly see why b[1, ,] doesn't result in a matrix. > drop(matrix(1:6, 6, 1)) [1] 1 2 3 4 5 6 Steve On Fri, 29 Sept 2023 at 23:28, Joseph Wood <jwood...@gmail.com> wrote: > > 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@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