Database Reference
In-Depth Information
[,1] [,2] [,3] [,4]
[1,] 0 0 0 0
[2,] 0 0 0 0
[3,] 0 0 0 0
A two-dimensional array is known as a
matrix
. The following code initializes
a matrix to hold the quarterly sales for the three regions. The parameters
nrow
and
ncol
define the number of rows and columns, respectively, for the
sales_matrix
.
sales_matrix <- matrix(0, nrow = 3, ncol = 4)
sales_matrix
[,1] [,2] [,3] [,4]
[1,] 0 0 0 0
[2,] 0 0 0 0
[3,] 0 0 0 0
R provides the standard matrix operations such as addition, subtraction, and
multiplication, as well as the transpose function
t()
and the inverse matrix
function
matrix.inverse()
included in the
matrixcalc
package. The
following R code builds a 3 × 3 matrix, M, and multiplies it by its inverse to obtain
the identity matrix.
library(matrixcalc)
M <- matrix(c(1,3,3,5,0,4,3,3,3),nrow = 3,ncol = 3)
# build
a 3x3 matrix
M %*% matrix.inverse(M)
# multiply M by inverse(M)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
Data Frames
Similar to the concept of matrices, data frames provide a structure for storing
and accessing several variables of possibly different data types. In fact, as the
is.data.frame()
function indicates, a data frame was created by the
read.csv()
function at the beginning of the chapter.