It is also important to understand the difference
between a vector of length
n
(which has no
attributes, except maybe names) and a matrix of
dimension
c
(n,1)
or
c
(1,n)
.
> c
[1] 1 2 3
> matrix(c,1,3)
[,1] [,2] [,3]
[1,]
1
2
3
> matrix(c,3,1)
[,1]
[1,]
1
[2,]
2
[3,]
3
9
If we format a vector to a matrix, the first column
gets filled first, then
the second column, and so
on. This is known as
column-major-order
. If we
want
row-major order
, we have to ask.
> matrix(1:6,3,2)
[,1] [,2]
[1,]
1
4
[2,]
2
5
[3,]
3
6
> matrix(1:6,3,2,byrow=TRUE)
[,1] [,2]
[1,]
1
2
[2,]
3
4
[3,]
5
6
10
Special Matrices
> diag(3)
[,1] [,2] [,3]
[1,]
1
0
0
[2,]
0
1
0
[3,]
0
0
1
> matrix(1,2,3)
[,1] [,2] [,3]
[1,]
1
1
1
[2,]
1
1
1
> diag(1:2)
[,1] [,2]
[1,]
1
0
[2,]
0
2
11
In statistics, we are mostly interested in two types
of matrices:
positive semi-definite real symmetric
matrices
and
rectangular matrices
in which
columns correspond to variables and row
correspond to observations.
Such a rectangular matrix is formalized more
appropriately in R as a data-frame. But because
of the prominence of matrices of this type, we
will sometimes treat matrices as vectors of
columns, although the matrix concept itself is
perfectly symmetric.
12