More on the Vector/Matrix Distinction
At the beginning of the chapter, I said that a matrix is just a vector but with two additional attributes: the number of rows and the number of columns. Here, we’ll take a closer look at the vector nature of matrices. Consider this example:
> z <- matrix(1:8,nrow=4) > z [,1] [,2] [1,] 1 5 [2,] 2 6 [3,] 3 7 [4,] 4 8
As z
is still a vector, we can query its length:
> length(z) [1] 8
But as a matrix, z
is a bit more than a vector:
> class(z) [1] "matrix" > attributes(z) $dim [1] 4 2
In other words, there actually is a matrix
class, in the object-oriented programming sense. As noted in Chapter 1, most of R consists of S3 classes, whose components are denoted by dollar signs. The matrix
class has one attribute, named ...
Get The Art of R Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.