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
I create a new vectorr called x1. No headres
x1 <- c(3,5,7)
names (x1)
## 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"
I assigned a name to each element of vector x1
names(x1) <- c("AA","BC","CD")
names(x1)
## [1] "AA" "BC" "CD"
x
## a b ab
## 1 2 4
Show x1
x1
## AA BC CD
## 3 5 7
We can remove the names from a vector by assigning NULL:
names(x) <- NULL
x
## [1] 1 2 4
Removed elements name from vector x1
names(x1) <- NULL
x1
## [1] 3 5 7
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
I made reference to elements of the vector by name, in this case CD, which is 7
x1 <- c(3,5,7)
names(x1) <- c("AA","BC","CD")
x1["CD"]
## CD
## 7
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
Matrix crceated with random values and 4 rows and 2 columns
y1<-matrix(c(9,5,3,7,6,1,2,8),nrow=4,ncol=2)
y1
## [,1] [,2]
## [1,] 9 6
## [2,] 5 1
## [3,] 3 2
## [4,] 7 8
#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
Here we crate the same matrix for y1 and its not necessary add the columns since R would know it by itself
y1<-matrix(c(9,5,3,7,6,1,2,8),nrow=4)
y1
## [,1] [,2]
## [1,] 9 6
## [2,] 5 1
## [3,] 3 2
## [4,] 7 8
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
Getting the second column of y1
y1[,2]
## [1] 6 1 2 8
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
Another way of create a matrix
y1 <- matrix(nrow=4,ncol=2)
y1
## [,1] [,2]
## [1,] NA NA
## [2,] NA NA
## [3,] NA NA
## [4,] 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
Add values to the y1 matrix by putting row number first and column number second
y1[1,1] <- 9
y1[2,1] <- 5
y1[1,2] <- 6
y1[2,2] <- 1
y1[3,1] <- 3
y1[3,2] <- 2
y1[4,1] <- 7
y1[4,2] <- 8
y1
## [,1] [,2]
## [1,] 9 6
## [2,] 5 1
## [3,] 3 2
## [4,] 7 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(1,2,3,4,5,6),nrow=2,byrow=T)
m
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
Creating a matrix and adding the amount of column depending how many rows we define
m1 <- matrix(c(4,7,5,2,9,6,3,1,2,8),nrow=2,byrow=T)
m1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 4 7 5 2 9
## [2,] 6 3 1 2 8
#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
Multiply y1 times 3
3*y1
## [,1] [,2]
## [1,] 27 18
## [2,] 15 3
## [3,] 9 6
## [4,] 21 24
y+y # mathematical matrix addition
## [,1] [,2]
## [1,] 2 6
## [2,] 4 8
Adding y1 plus y1
y1+y1
## [,1] [,2]
## [1,] 18 12
## [2,] 10 2
## [3,] 6 4
## [4,] 14 16
#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.
I create z1 with 4 rows and 3 cols and adding a value to each element
z1 <- matrix(nrow=4,ncol=3)
z1[1,1] <- 3
z1[2,1] <- 5
z1[1,2] <- 2
z1[2,2] <- 5
z1[3,1] <- 9
z1[3,2] <- 7
z1[3,3] <- 4
z1[1,3] <- 1
z1[2,3] <- 3
z1[4,1] <- 8
z1[4,2] <- 5
z1[4,3] <- 6
z1
## [,1] [,2] [,3]
## [1,] 3 2 1
## [2,] 5 5 3
## [3,] 9 7 4
## [4,] 8 5 6
Geting column numbers 1 and 2 of every row
z[,1:2]
## [,1] [,2]
## [1,] 1 1
## [2,] 2 1
## [3,] 3 0
## [4,] 4 0
y
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
y1
## [,1] [,2]
## [1,] 9 6
## [2,] 5 1
## [3,] 3 2
## [4,] 7 8
y[2:1,]
## [,1] [,2]
## [1,] 2 4
## [2,] 1 3
Getting row 1 of all columns
y1[1,]
## [1] 9 6
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
Creat a matrix x1 with 6 elements
x1<-c(12,8,16,10,5,20)
x1
## [1] 12 8 16 10 5 20
x <- c(x,20) # append 20
x
## [1] 1 12 5 13 16 8 20
Add 17 to the x1 matrix
x1 <- c(x1,17)
x1
## [1] 12 8 16 10 5 20 17
x <- c(x[1:3],20,x[4:6]) # insert 20
x
## [1] 1 12 5 20 13 16 8
Insert 17 between the 3rd and 4th element of x1
x1 <- c(x1[1:3],17,x1[4:6])
x1
## [1] 12 8 16 17 10 5 20
x <- x[-2:-4] # delete elements 2 through 4
x
## [1] 1 13 16 8
I deleted elements 2 through 4 from x1
x1 <- x1[-2:-4]
x1
## [1] 12 10 5 20
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
two <- c(2,2,2,2)
two
## [1] 2 2 2 2
z
## [,1] [,2] [,3]
## [1,] 1 1 1
## [2,] 2 1 0
## [3,] 3 0 1
## [4,] 4 0 0
z1
## [,1] [,2] [,3]
## [1,] 3 2 1
## [2,] 5 5 3
## [3,] 9 7 4
## [4,] 8 5 6
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:
I created a new matrix by combining a column of two with the columns of z1
cbind(two,z1)
## two
## [1,] 2 3 2 1
## [2,] 2 5 5 3
## [3,] 2 9 7 4
## [4,] 2 8 5 6
cbind(z,one)
## one
## [1,] 1 1 1 1
## [2,] 2 1 0 1
## [3,] 3 0 1 1
## [4,] 4 0 0 1
I create the same matrix but with two colum on the other side
cbind(z1,two)
## two
## [1,] 3 2 1 2
## [2,] 5 5 3 2
## [3,] 9 7 4 2
## [4,] 8 5 6 2
z <- cbind(one,z)
z1 <- cbind(two,z1)
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
Recycling z1
cbind(2,z1)
## two
## [1,] 2 2 3 2 1
## [2,] 2 2 5 5 3
## [3,] 2 2 9 7 4
## [4,] 2 2 8 5 6
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
Created a new matrix using cbind
q1<- cbind(c(2,9),c(4,11))
q1
## [,1] [,2]
## [1,] 2 4
## [2,] 9 11
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
New matrix quicker way
m1 <- matrix(1:10,nrow=5)
m1
## [,1] [,2]
## [1,] 1 6
## [2,] 2 7
## [3,] 3 8
## [4,] 4 9
## [5,] 5 10
m <- m[c(1,3),]
m
## [,1] [,2]
## [1,] 1 4
## [2,] 3 6
Matrix m1 row 1 and 3, all columns
m1 <- m1[c(1,3),]
m1
## [,1] [,2]
## [1,] 1 6
## [2,] 3 8