1.
9 Matrix
A matrix is a vector represented and accessible in two dimensions.Therefore,
what applies to vectors is most likely to apply to a matrix. A matrix A as an
m × n matrix; that is, A will have exactly m rows and n columns. This means
A will have a total of mn entries, with each entry aij having a unique position
given by its specific row (i = 1, 2, ..., m) and column (j = 1, 2, ..., n).
A=
a11 a12 .. .. .. a1m
a21 a22 .. .. .. a2m
.. .. .. .. .. ..
.. .. .. .. .. ..
.. .. .. .. .. ..
an1 an2 .. .. .. amn
26 CHAPTER 1. INTRODUCTION TO R
1.9.1 Creating a matrix
We can call matrix() to create a matrix from a vector.By default the matrix
elements are filled coulmnwise from the given vector. If we want to store the
given elements rowwise, the argument byrow should be made ”TRUE.”
Syntax:
matrix(data, nrow, ncol, byrow, dimname), where
data - The first argument in matrix function is data. It is the input vector
which is the data elements of the matrix.
nrow - The second argument is the number of rows which we want to create
in the matrix.
ncol - The third argument is the number of columns which we want to
create in the matrix.
byrow - The byrow parameter is a logical clue. If its value is true, then
the input vector elements are arranged by row.
dim−name - The dim name parameter is the name assigned to the rows
and columns.
Example:
1. A < −matrix(c(1, 5, 3, 0, −1, 3, 6, 2, 4), ncol = 3)
Output
2. B < −matrix(c(1, 5, 3, 0, −1, 3, 6, 2, 4), nrow = 3, byrow = T RUE)
Output
3. B < −matrix(c(1, 5, 3, 0, −1, 3, 6, 2, 4), nrow = 3, byrow = F ALSE)
Output
1.9. MATRIX 27
1.9.2 Naming rows and columns
By default, creating a matrix does not automatically give names to its rows
and columns. Sometimes, it is useful and straightforward to do so when differ-
ent rows and columns have different meanings. We can give row names and/or
column names when creating the matrix.
Example:
(1)matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, byrow = T RUE, dimnames = list(c(”r1”,
”r2”, ”r3”), c(”c1”, ”c2”, ”c3”)))
Output
Alternatively, we can use row names and/or column names after the matrix is
created.
(2) m1 < −matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), ncol = 3)
rownames(m1) < −c(”r1”, ”r2”, ”r3”)
colnames(m1) < −c(”c1”, ”c2”, ”c3”)
Output
1.9.3 Subsetting a matrix
Extracting (or)accessing elements from matrices in R is much like extracting
elements from vectors. This is called matrix subsetting. Elements of a ma-
trix can be accessed by using the column and row index of the element.Element
extraction still uses the square-bracket operator [ ], but now it must be per-
formed with both a row and a column position, given strictly in the order of
[row, column].
Examples:
# Define the column and row names.
rownames = c(”row1”, ”row2”, ”row3”, ”row4”)
colnames = c(”col1”, ”col2”, ”col3”)
# Create the matrix.
P < −matrix(c(3 : 14), nrow = 4, byrow = T RUE, dimnames = list(rownames,
28 CHAPTER 1. INTRODUCTION TO R
colnames))
Output
# Access the element at 3rd column and 1st row.
print(P[1, 3])
Output [1] 5
# Access the element at 2nd column and 4th row.
print(P[4, 2])
Output [1] 13
# Access only the 2nd row.
print(P[2, ])
Output
col1 col2 col3
678
# Access only the 3rd column.
print(P[, 3])
Output:
row1 row2 row3 row4
5 8 11 14
# We can subset it with a range of positions.
P[1 : 2, 2 : 3]
Output
# Access 2nd and 3rd column.
P[, 2 : 3]
Output
1.9. MATRIX 29
Note that the matrix has row names and column names, and we can use
character vectors to subset it.
P[c(”row1”, ”row3”), c(”col1”, ”col3”)]
Output
1.9.4 Omitting and Overwriting Elements from a matrix:
To delete or omit elements from a matrix, you again use square brackets,but
this time with negative indexes.
Examples:
1. P[−1, ]
Output
2. P[, −2]
Output
3. P[−1, −2]
Output1.9. MATRIX 31
A matrix is a vector represented and accessible in two dimensions; how-
ever, it is still a vector in its nature. This allows us to use a one-dimensional
accessor for vectors to subset a matrix.
Examples:
1. P[3]
Output [1] 7
2. P[3 : 6]
Output [1] 7 12 11 2
An inequality will return another logical matrix of equal size.
Example:P > 4
Output
We can use an equal-sized logical matrix for subsetting as if it is a vector.
Example:P[P > 4]
Output [1] 7 12 11 11 13 7 14
1.9.5 Arithmetic Operations on Matrices
All arithmetic operators for vectors also work with matrices as if they were
vectors. These operators perform calculations element-wise, except for matrix-
only operators, such as matrix product, % ∗ %.
Examples
1. P + P
Output
2. P − 2 ∗ P
Output
32 CHAPTER 1. INTRODUCTION TO R
3. P ∗ P
Output
4. P
Output
5. P/P
Output
6. Q < −matrix(1 : 9, nrow = 3, ncol = 3)
Output
Q% ∗ %Q
Output
1.9. MATRIX