SST 301 • UNIT 1 • Statistics
Matrices in R
Topics covered: Matrix Creation & Indexing, Matrix Arithmetic & Manipulations, Linear Algebra Operations, Advanced Matrix Products
Syllabus Study Notes (Core Overview)
Free Overview Preview1. Concept and Use Case
A matrix in R is a two-dimensional data structure arranged in a rectangular layout. It can be understood as a vector extended into two dimensions, with elements positioned by row and column. A defining constraint is that a matrix may contain only one data type; all entries must share the same mode (for example, all numeric). This homogeneity is what makes matrices suitable for arithmetic and linear-algebra operations.
Matrices are the natural computational object for many statistical and computational tasks. In the broader course context, they underpin linear programming, confidence-interval estimation, principal component analysis, quality control, and life-table work. Within this topic, the central uses are:
- storing rectangular data (for example, observations by variables) in a single object;
- performing element-wise arithmetic and true matrix multiplication;
- solving linear-algebra problems such as inversion, determinants, and eigen-decomposition;
- building structured matrices (diagonal and identity matrices) used in transformations;
- forming Kronecker (direct) products of matrices of arbitrary compatible dimensions.
The eigenvalue problem is especially important: eigenvalues and eigenvectors appear prominently in statistical and computational fields that require transformations of linear systems or study the evolution of systems from an initial point.
2. Data, Syntax, and Core Concepts
2.1 Creating a matrix with matrix()
The function matrix() constructs a matrix from a vector of values together with dimension settings. The key arguments are:
- the data vector (the values to be arranged);
nrow, the number of rows;ncol, the number of columns;byrow, a logical flag controlling the fill order.
By default the vector fills the matrix column by column. Setting byrow = TRUE fills it row by row.
# creating a simple matrix
A <- matrix(1:6, nrow = 3, ncol = 2)
B <- matrix(1:6, nrow = 3, ncol = 2, byrow = TRUE)
Here A receives the values column-wise, while B receives the same values row-wise. The two matrices therefore differ in arrangement even though they contain identical entries.
2.2 Assigning row and column names
Names are attached with colnames() and rownames(). The number of names supplied must match the corresponding dimension.
M <- matrix(1:20, ncol = 4)
colnames(M) <- c("A", "B", "C", "D")
rownames(M) <- c("E", "F", "G", "H", "I")
Because M has four columns and five rows, four column names and five row names are required.
2.3 Indexing and subsetting
Elements are addressed with the bracket notation [row, column]. Leaving one index blank selects an entire row or column.
# creating a matrix
MatA <- matrix(c(2, 8, 9, 7), nrow = 2, ncol = 2, byrow = TRUE)
# extracting the element in first row and second column
MatA[1, 2]
# extracting entire row
MatA[1, ]
# extracting entire column
MatA[, 2]
More than one row or column can be extracted at a time by supplying vectors of indices in place of a single index.
2.4 Checking dimensions with dim()
The function dim() returns the dimensions of a matrix as a vector of length two: the number of rows followed by the number of columns.
# checking the dimension of a matrix
D <- matrix(c(4, 5, 7, 8), 2, 2, byrow = TRUE)
dim(D)
Dimension checking is essential before any arithmetic, because most matrix operations require compatible dimensions.
2.5 Replacing elements
Individual entries or whole rows can be overwritten by assignment through the bracket notation.
Matb <- matrix(1, 3, 3)
# replacing one element
Matb[1, 1] = 2
# replacing entire row
Matb[1, ] = 2
The first assignment changes a single entry; the second replaces every entry of the first row with the value 2.
Unlock Complete Study Notes & Exam Traps
The full guide includes formal proofs, worked exemplar calculations, high-yield exam traps, and examiner marking schemes. Continue studying this topic in Mentify Prep Studio.
Verified Examination Problems
10 verified questions in catalogQuestion 1 (13 marks)
Consider the linear model $y = X\beta + \varepsilon$, where $X$ is an $n \times p$ design matrix of full column rank, $\varepsilon \sim N(0, \sigma^2 I_n)$, and the hat matrix is $H = X(X^T X)^{-1}X^T$. Let $\hat{y} = Hy$ and $e = y - \hat{y}$. (i) Define the leverage of the$i$th observation and explain its role in ordinary least squares. [2 Marks] (ii) Derive the expectation and covariance matrix of the ordinary residual vector$e$. [4 Marks]
(iii) Define the externally studentized (deleted) residual and explain how it differs from the internally studentized residual. [3 Marks]
(iv) Describe the ideal residual-versus-fitted plot and state which linear model assumptions it can detect. [4 Marks]
Question 1 (10 marks)
A researcher records the daily maximum temperatures (in degrees Celsius) for three weather stations over four days. The data are stored in an R matrix as follows:
T <- matrix(c(23, 25, 21, 24,
19, 22, 20, 18,
30, 28, 31, 29),
nrow = 3, byrow = TRUE)
(i) Write down the R code to display the element in the second row and third column of T. (2 marks)
(ii) Write down the R code to extract the entire second row of T as a vector. (2 marks)
(iii) Write down the R code to extract the submatrix consisting of the first and third rows and the second and fourth columns. (3 marks)
(iv) Write down the R code to replace the element in the third row and first column with the value 27. (3 marks)
Access all authentic past questions, interactive AI hint generation, and full marking rubrics in the course studio.
Unlock All Questions in Studio →