Julius Schmid
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(3,7,5,82)
names(x)
NULL
Since the vector x contains only numerical values without names, the name(x) function returns NULL.
# Please create a vector x1 and follow the steps of this exercise.
x1 <- c(25, 2, 9, 16, 42)
In this example, we consider a global ranking for different sports teams. We can assign or query vector element names via the names() function:
names(x) <- c("Team 1","Team 2","Team 3", "Team 4")
names(x)
[1] "Team 1" "Team 2" "Team 3" "Team 4"
When we return x now, the values are represented together with the new assigned column names:
x
Team 1 Team 2 Team 3 Team 4
3 7 5 82
We can remove the names from a vector by assigning NULL:
names(x) <- NULL
x
[1] 3 7 5 82
We can even reference elements of the vector by name:
x <- c(3,7,5,82)
names(x) <- c("Team 1","Team 2","Team 3", "Team 4")
x["Team 3"]
Team 3
5
The output is only the ranking value of Team 3, which is 5.
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.
Creating Matrices
Let us create a 3x3 matrix, using the parameters nrow (number of rows) and ncol (number of columns) to transform a 9-element vector.
y <-matrix(c(25,2,8,1,9,5,4,7,8),nrow=3,ncol=3)
y
[,1] [,2] [,3]
[1,] 25 1 4
[2,] 2 9 7
[3,] 8 5 8
Since we specified the matrix entries in the preceding example, and there were nine of them, we did not need to specify both ncol and nrow; just nrow or ncol would have been enough. Having nine elements in all, in three rows, implies two columns:
y <- matrix(c(25,2,8,1,9,5,4,7,8),nrow=3)
y
[,1] [,2] [,3]
[1,] 25 1 4
[2,] 2 9 7
[3,] 8 5 8
Note that when we then print out y, R shows us its notation for rows and columns. For instance, [,1] means the entirety of column 1, as can be seen in this check:
y[,1]
[1] 25 2 8
Another way to build y is to specify elements individually:
y <- matrix(nrow=3,ncol=3)
y
[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA NA NA
[3,] NA NA NA
y[1,1] <- 25
y[1,2] <- 1
y[1,3] <- 4
y[2,1] <- 2
y[2,2] <- 9
y[2,3] <- 7
y[3,1] <- 8
y[3,2] <- 5
y[3,3] <- 8
y
[,1] [,2] [,3]
[1,] 25 1 4
[2,] 2 9 7
[3,] 8 5 8
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(10,9,8,7,6,5),nrow=3,byrow=T)
m
[,1] [,2]
[1,] 10 9
[2,] 8 7
[3,] 6 5
We see that, by setting byrow = T (True), the matrix will be filled up by the rows and not by the columns. This means that the second element in the input vector will not be set in the first column (as the matrix entry with index [2,1]), but rather in the first row (as the matrix entry with index [1,2])
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] [,3]
[1,] 659 54 139
[2,] 124 118 127
[3,] 274 93 131
Since y is a 3x3 matrix, the result is a 3x3 matrix as well.
5*y # mathematical multiplication of matrix by scalar
[,1] [,2] [,3]
[1,] 125 5 20
[2,] 10 45 35
[3,] 40 25 40
Here, each matrix entry is multiplied by the scalar 5.
y+y # mathematical matrix addition
[,1] [,2] [,3]
[1,] 50 2 8
[2,] 4 18 14
[3,] 16 10 16
Each matrix entry of the first matrix (y) is added to the corresponding entry of the second matrix (here also y). For a matrix addition, the two matrices must have the same dimension. Note that y+y is the same as 2*y for the quadratic matrix y.
Matrix Indexing
Let us say that we have a matrix z:
z <- matrix(nrow=5,ncol=3)
z[1,1] <- 0
z[2,1] <- 3
z[1,2] <- 7
z[2,2] <- 6
z[3,1] <- 2
z[3,2] <- 3
z[3,3] <- 1
z[1,3] <- 1
z[2,3] <- 2
z[4,1] <- 0
z[4,2] <- 8
z[4,3] <- 5
z[5,1] <- 3
z[5,2] <- 1
z[5,3] <- 2
We return the matrix just by writing z in the command shell.
z
[,1] [,2] [,3]
[1,] 0 7 1
[2,] 3 6 2
[3,] 2 3 1
[4,] 0 8 5
[5,] 3 1 2
Let us obtain the submatrix of z consisting of all elements with column numbers 1 and 2 and any row number. This extracts the second and third columns.
z[,1:2]
[,1] [,2]
[1,] 0 7
[2,] 3 6
[3,] 2 3
[4,] 0 8
[5,] 3 1
Let us now extract rows instead of columns. We return the rows 3 through 5 this time, where we print all the columns.
z[3:5,]
[,1] [,2] [,3]
[1,] 2 3 1
[2,] 0 8 5
[3,] 3 1 2
Let us consider matrix y again:
y
[,1] [,2] [,3]
[1,] 25 1 4
[2,] 2 9 7
[3,] 8 5 8
Create a matrix that has the second row of y as the first row, and the first row of y as its second row:
y[2:1,]
[,1] [,2] [,3]
[1,] 2 9 7
[2,] 25 1 4
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(4,3,1,6)
x
[1] 4 3 1 6
Add the value 13 at the end of vector x:
x <- c(x,13) # append 13
x
[1] 4 3 1 6 13
Next, insert the value 80 between the first two and last three entries of x:
x <- c(x[1:2],80,x[3:5]) # insert 80
x
[1] 4 3 80 1 6 13
Using the minus sign, we can also delete elements from the vector x:
x <- x[-3:-5] # delete elements 3 through 5
x
[1] 4 3 13
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.
zeros<-c(0,0,0,0,0)
zeros
[1] 0 0 0 0 0
z
[,1] [,2] [,3]
[1,] 0 7 1
[2,] 3 6 2
[3,] 2 3 1
[4,] 0 8 5
[5,] 3 1 2
cbind(zeros,z)
zeros
[1,] 0 0 7 1
[2,] 0 3 6 2
[3,] 0 2 3 1
[4,] 0 0 8 5
[5,] 0 3 1 2
Here, cbind() creates a new matrix by combining a column of 0s 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,zeros)
zeros
[1,] 0 7 1 0
[2,] 3 6 2 0
[3,] 2 3 1 0
[4,] 0 8 5 0
[5,] 3 1 2 0
Note, too, that we could have relied on recycling:
cbind(0,z)
[,1] [,2] [,3] [,4]
[1,] 0 0 7 1
[2,] 0 3 6 2
[3,] 0 2 3 1
[4,] 0 0 8 5
[5,] 0 3 1 2
You can also use the rbind() and cbind() functions as a quick way to create small matrices. Here’s an example:
q <- cbind(c(4,6,8),c(1,5,9))
q
[,1] [,2]
[1,] 4 1
[2,] 6 5
[3,] 8 9
You can delete rows or columns by reassignment, too:
m <- matrix(1:9,nrow=3)
m
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
Lastly, return the first and the third row of the matrix m and override m with the new sub-matrix.
m <- m[c(1,3),]
m
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 3 6 9