Skip to content
Prev 61924 / 63421 Next

Use of `[` with array and resulting class

?s 17:33 de 29/09/2023, Joseph Wood escreveu:
Hello,

You are using the default behavior for objects of class "array" (and 
"matrix"), which is drop = TRUE. See ?`[`.
You probably want the second example below.


b <- array(1:6, dim = c(1, 6, 1))
# the default is drop = TRUE
b[1,,]
#> [1] 1 2 3 4 5 6

# keep the dim attribute
b[1, , , drop = FALSE]
#> , , 1
#>
#>      [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,]    1    2    3    4    5    6


Hope this helps,

Rui Barradas