Vector Element Names
The elements of a vector can optionally be given names. For example, say we have a 50-element vector showing the population of each state in the United States. We could name each element according to its state name, such as “Montana” and “New Jersey”. This in turn might lead to naming points in plots, and so on.
x <- c(896,254,85641)
names(x)
NULL
-> Null since there are no ‘headers’ specified
# Please create a vector x1 and follow the steps of this exercise.
x1 <- c(856123,97845,984561)
We can assign or query vector element names via the names() function:
names(x) <- c("a","b","ab")
names(x)
[1] "a" "b" "ab"
-> with ‘headers’ being defined, the names function returns those assigned ‘headers’
x
a b ab
896 254 85641
-> x now conatains headers with respective values
We can remove the names from a vector by assigning NULL:
names(x) <- NULL
x
[1] 896 254 85641
-> ‘headers’ are removed by assigning NULL to names
We can even reference elements of the vector by name:
x <- c(1,2,4)
names(x) <- c("a","b","ab")
x["b"]
b
2
-> returns the element (2) with the ‘header’ b within x
MATRICES AND ARRAYS
A matrix is a vector with two additional attributes: the number of rows and the number of columns. Since matrices are vectors, they also have modes, such as numeric and character.
Cretaing Matrices
y<-matrix(c(1,2,3,4),nrow=2,ncol=2)
y
[,1] [,2]
[1,] 1 3
[2,] 2 4
-> creates 2-dimensional matrix with two rows and two columns
#Please create a matrix y1 and follow the steps below
y1 <- matrix(c(852,258,456,159), nrow=2, ncol=2)
y1
[,1] [,2]
[1,] 852 456
[2,] 258 159
Since we specified the matrix entries in the preceding example, and there were four of them, we did not need to specify both ncol and nrow; just nrow or ncol would have been enough. Having four elements in all, in two rows, implies two columns:
y <- matrix(c(1,2,3,4),nrow=2)
y
[,1] [,2]
[1,] 1 3
[2,] 2 4
Note that when we then print out y, R shows us its notation for rows and columns. For instance, [,2] means the entirety of column 2, as can be seen in this check:
y[,2]
[1] 3 4
-> returns column 2
Another way to build y is to specify elements individually:
y <- matrix(nrow=2,ncol=2)
y
[,1] [,2]
[1,] NA NA
[2,] NA NA
-> creates empty matrix with 2 rows and 2 columns
y[1,1] <- 1
y[2,1] <- 2
y[1,2] <- 3
y[2,2] <- 4
y
[,1] [,2]
[1,] 1 3
[2,] 2 4
-> assigns values to specific locations within the matrix
Note that we do need to warn R ahead of time that y will be a matrix and give the number of rows and columns.
Though internal storage of a matrix is in column-major order, you can set the byrow argument in matrix() to true to indicate that the data is coming in row-major order. Here’s an example of using byrow:
m <- matrix(c(1,2,3,4,5,6),nrow=2,byrow=T)
m
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
#Create a matrix m1 and recreate a similar case scenario to the one above.
m1 <- matrix(c(195,2745,335,4719,5371,6195),nrow=2,byrow=T)
m1
[,1] [,2] [,3]
[1,] 195 2745 335
[2,] 4719 5371 6195
-> have the matrix created in row-major order
General Matrix Operations
Performing Linear Algebra Operations on Matrices
You can perform various linear algebra operations on matrices, such as matrix multiplication, matrix scalar multiplication, and matrix addition. Using y from the preceding example, here is how to perform those three operations:
y %*% y # mathematical matrix multiplication
[,1] [,2]
[1,] 7 15
[2,] 10 22
3*y # mathematical multiplication of matrix by scalar
[,1] [,2]
[1,] 3 9
[2,] 6 12
y+y # mathematical matrix addition
[,1] [,2]
[1,] 2 6
[2,] 4 8
y1 %*% y1 # mathematical matrix multiplication
[,1] [,2]
[1,] 843552 461016
[2,] 260838 142929
3*y1 # mathematical multiplication of matrix by scalar
[,1] [,2]
[1,] 2556 1368
[2,] 774 477
y1+y1 # mathematical matrix addition
[,1] [,2]
[1,] 1704 912
[2,] 516 318
Matrix Indexing
Let us say that we have a matrix z:
z <- matrix(nrow=4,ncol=3)
z[1,1] <- 1
z[2,1] <- 2
z[1,2] <- 1
z[2,2] <- 1
z[3,1] <- 3
z[3,2] <- 0
z[3,3] <- 1
z[1,3] <- 1
z[2,3] <- 0
z[4,1] <- 4
z[4,2] <- 0
z[4,3] <- 0
z
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 2 1 0
[3,] 3 0 1
[4,] 4 0 0
-> 2-dimensional matrix with four rows and three columns
Let us obtain the submatrix of z consisting of all elements with column numbers 2 and 3 and any row number. This extracts the second and third columns.
z[,2:3]
[,1] [,2]
[1,] 1 1
[2,] 1 0
[3,] 0 1
[4,] 0 0
-> extracts last two columns and all four rows
Let us now extract rows instead of columns.
#Create amatrix z1 and recreate a similar case scenario.
z1 <- matrix(nrow=4,ncol=3)
z1[1,1] <- 18852
z1[2,1] <- 20235
z1[1,2] <- 156122
z1[2,2] <- 1651
z1[3,1] <- 35631
z1[3,2] <- 231
z1[3,3] <- 123165
z1[1,3] <- 1856213
z1[2,3] <- 213165
z1[4,1] <- 4565161
z1[4,2] <- 5613121
z1[4,3] <- 32198
z1
[,1] [,2] [,3]
[1,] 18852 156122 1856213
[2,] 20235 1651 213165
[3,] 35631 231 123165
[4,] 4565161 5613121 32198
z1[2:1,]
[,1] [,2] [,3]
[1,] 20235 1651 213165
[2,] 18852 156122 1856213
-> returns the second and first rows for all three columns of matrix z1
Adding and Deleting Matrix Rows and Columns Technically, matrices are of fixed length and dimensions, so we cannot add or delete rows or columns. However, matrices can be reassigned, and thus we can achieve the same effect as if we had directly done additions or deletions.
Changing the Size of a Matrix
x<-c(1,12,5,13,16,8)
x
[1] 1 12 5 13 16 8
x <- c(x,20) # append 20
x
[1] 1 12 5 13 16 8 20
-> appends 20 to the existing x vector
x <- c(x[1:3],20,x[4:6]) # insert 20
x
[1] 1 12 5 20 13 16 8
-> inserts 20 between the 3rd and 4th elemnt of x
x <- x[-2:-4] # delete elements 2 through 4
x
[1] 1 13 16 8
-> deletes elements 2 through 4
Analogous operations can be used to change the size of a matrix. For instance, the rbind() (row bind) and cbind() (column bind) functions let you add rows or columns to a matrix.
one<-c(1,1,1,1)
one
[1] 1 1 1 1
z
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 3 0 1
cbind(one,z)
one
[1,] 1 1 1 1
[2,] 1 2 1 0
[3,] 1 3 0 1
[4,] 1 4 0 0
Here, cbind() creates a new matrix by combining a column of 1s with the columns of z. We choose to get a quick printout, but we could have assigned the result to z (or another variable), as follows:
cbind(z,one)
one
[1,] 1 1 1 1
[2,] 2 1 0 1
[3,] 3 0 1 1
[4,] 4 0 0 1
-> binds one to the end of z
z <- cbind(one,z)
-> binds z to end of one and assigns new matrix to z
Note, too, that we could have relied on recycling:
cbind(1,z)
one
[1,] 1 1 1 1 1
[2,] 1 1 2 1 0
[3,] 1 1 3 0 1
[4,] 1 1 4 0 0
-> adds column of ones to beginning of z
You can also use the rbind() and cbind() functions as a quick way to create small matrices. Here’s an example:
q <- cbind(c(1,2),c(3,4))
q
[,1] [,2]
[1,] 1 3
[2,] 2 4
-> creates new matrix with cbind function
You can delete rows or columns by reassignment, too:
m <- matrix(1:6,nrow=3)
m
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
-> reassigns matrix to m and, therefore, deletes old, unused values
z <- z[c(1,3),]
z
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 3 0 1
-> returns first and thrid row of z