[Rd] class(<matrix>) |--> c("matrix", "arrary") [was "head.matrix ..."]
Dénes Tóth
toth@dene@ @end|ng |rom kogentum@hu
Fri Nov 15 22:38:56 CET 2019
Hi Abby,
On 11/15/19 10:19 PM, Abby Spurdle wrote:
>>> And indeed I think you are right on spot and this would mean
>>> that indeed the implicit class
>>> "matrix" should rather become c("matrix", "array").
>>
>> I've made up my mind (and not been contradicted by my fellow R
>> corers) to try go there for R 4.0.0 next April.
>
> I'm not enthusiastic about matrices extending arrays.
> If a matrix is an array, then shouldn't all vectors in R, be arrays too?
The main distinguishing feature of matrices (and arrays) vs vectors is
that they have a dimension attribute.
x <- as.list(letters[1:8]) # just to show that it generalizes not only
to atomic vectors
is.vector(x) # TRUE
inherits(x, "matrix") # FALSE
dim(x) <- c(2, 4)
is.vector(x) # FALSE
inherits(x, "matrix") # TRUE
inherits(x, "array") # FALSE, but should be TRUE for consistency
dim(x) <- c(2, 2, 2)
is.vector(x) # FALSE
inherits(x, "matrix") # FALSE
inherits(x, "array") # TRUE
A matrix should be really nothing else just an array where
length(dim(x)) == 2L.
IMHO the only special object which has dimension attribute but is not a
special case of arrays is the data.frame.
Denes
>
>> #mockup
>> class (1)
> [1] "numeric" "array"
>
> Which is a bad idea.
> It contradicts the central principle that R uses "Vectors" rather than "Arrays".
> And I feel that matrices are and should be, a special case of vectors.
> (With their inheritance from vectors taking precedence over anything else).
>
> If the motivation is to solve the problem of 2D arrays, automatically
> being mapped to matrices:
>
>> class (array (1, c (2, 2) ) )
> [1] "matrix"
>
> Then wouldn't it be better, to treat 2D arrays, as a special case, and
> leave matrices as they are?
>
>> #mockup
>> class (array (1, c (2, 2) ) )
> [1] "array2d" "matrix" "array"
>
> Then 2D arrays would have access to both matrix and array methods...
>
> Note, I don't want to enter into (another) discussion on the
> differences between implicit class and classes defined via a class
> attribute.
> That's another discussion, which has little to do with my points above.
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
More information about the R-devel
mailing list