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(1,2,4)
names(x)
NULL
# Please create a vector x1 and follow the steps of this exercise.
We can assign or query vector element names via the names() function:
names(x) <- c("a","b","ab")
names(x)
[1] "a" "b" "ab"
x
a b ab
1 2 4
We can remove the names from a vector by assigning NULL:
names(x) <- NULL
x
[1] 1 2 4
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
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
#Please create a matrix y1 and follow the steps below
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
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
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
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.
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
#Perform the operations shown above using y1
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
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
Let us now extract rows instead of columns.
#Create amatrix z1 and recreate a similar case scenario.
y
[,1] [,2]
[1,] 1 3
[2,] 2 4
y[2:1,]
[,1] [,2]
[1,] 2 4
[2,] 1 3
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
x <- c(x[1:3],20,x[4:6]) # insert 20
x
[1] 1 12 5 20 13 16 8
x <- x[-2:-4] # delete elements 2 through 4
x
[1] 1 13 16 8
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,] 2 1 0
[3,] 3 0 1
[4,] 4 0 0
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
z <- cbind(one,z)
Note, too, that we could have relied on recycling:
cbind(1,z)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 2 1 0
[3,] 1 3 0 1
[4,] 1 4 0 0
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
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
m <- m[c(1,3),]
m
[,1] [,2]
[1,] 1 4
[2,] 3 6