October 2011
Beginner to intermediate
400 pages
9h 30m
English
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 ...
Read now
Unlock full access