1. Create x1 & check for column names
x1 <- c(2,3,5)
names(x1)
## NULL
  1. Name columns x1; check for column names; show matrix
names(x1) <- c("a","b","ab")
names(x1)
## [1] "a"  "b"  "ab"
x1
##  a  b ab 
##  2  3  5
  1. Remove column names from a vector
names(x1) <- NULL
x1
## [1] 2 3 5
  1. Name columns & Reference elements of the vector by name
names(x1) <- c("a","b","ab")
x1["b"]
## b 
## 3
  1. Create y1 Matricies and show y1
y1<-matrix(c(2,3,4,5),nrow=2,ncol=2)
y1
##      [,1] [,2]
## [1,]    2    4
## [2,]    3    5
  1. IMPORTANT NOTE! 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
y1<-matrix(c(2,3,4,5),nrow=2)
y1
##      [,1] [,2]
## [1,]    2    4
## [2,]    3    5
  1. Show entire values for Column 2
y1[,2]
## [1] 4 5

8.Specify elements for MATRIX individually.

y2 <- matrix(nrow=2,ncol=2) #Note! we need to warn R ahead of time that 'y2' will be a matrix and give the number of rows and columns.
y2[1,1] <- 3
y2[2,1] <- 4
y2[1,2] <- 5
y2[2,2] <- 6
y2
##      [,1] [,2]
## [1,]    3    5
## [2,]    4    6
  1. IMPORTANT NOTE!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.
m1 <- matrix(c(2,3,4,5,6,7),nrow=2,byrow=T)
m1
##      [,1] [,2] [,3]
## [1,]    2    3    4
## [2,]    5    6    7
  1. Performing Linear Algebra Operations on Matrices
y1
##      [,1] [,2]
## [1,]    2    4
## [2,]    3    5
y1 %*% y1 # mathematical matrix multiplication
##      [,1] [,2]
## [1,]   16   28
## [2,]   21   37
3*y1 # mathematical multiplication of matrix by scalar
##      [,1] [,2]
## [1,]    6   12
## [2,]    9   15
y1+y1 # mathematical matrix addition
##      [,1] [,2]
## [1,]    4    8
## [2,]    6   10
  1. Matrix Indexing
z1 <- matrix(nrow=4,ncol=3) #create matrix 4x3 and assign values
z1[1,1] <- 2
z1[2,1] <- 3
z1[1,2] <- 2
z1[2,2] <- 2
z1[3,1] <- 4
z1[3,2] <- 1
z1[3,3] <- 2
z1[1,3] <- 2
z1[2,3] <- 1
z1[4,1] <- 5
z1[4,2] <- 1
z1[4,3] <- 1
z1
##      [,1] [,2] [,3]
## [1,]    2    2    2
## [2,]    3    2    1
## [3,]    4    1    2
## [4,]    5    1    1
z1[,2:3] #extracts all elements with column 2,3 and any row number
##      [,1] [,2]
## [1,]    2    2
## [2,]    2    1
## [3,]    1    2
## [4,]    1    1
z1[2:1,] #extract rows 2,1
##      [,1] [,2] [,3]
## [1,]    3    2    1
## [2,]    2    2    2
  1. IMORTANT NOTE! 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.
x2<-c(2,13,6,14,17,9)
x2
## [1]  2 13  6 14 17  9
x2 <- c(x2,22) # append 22
x2
## [1]  2 13  6 14 17  9 22
x2 <- c(x2[1:3],33,x2[4:6]) # insert 33
x2
## [1]  2 13  6 33 14 17  9
x2 <- x2[-2:-4] # delete elements 2 through 4
x2
## [1]  2 14 17  9
  1. 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.
two<-c(2,2,2,2)
two
## [1] 2 2 2 2
z1
##      [,1] [,2] [,3]
## [1,]    2    2    2
## [2,]    3    2    1
## [3,]    4    1    2
## [4,]    5    1    1
cbind(two,z1) #creates new matrix by combining a column of 2s with the columns of z
##      two      
## [1,]   2 2 2 2
## [2,]   2 3 2 1
## [3,]   2 4 1 2
## [4,]   2 5 1 1
z2 <- cbind(z1,two) #creates new matrix by combining a column of 2s with the columns of z and saves it to new DF
z2
##            two
## [1,] 2 2 2   2
## [2,] 3 2 1   2
## [3,] 4 1 2   2
## [4,] 5 1 1   2
cbind(2,z1) #same function as line 111 (reling on recycling)
##      [,1] [,2] [,3] [,4]
## [1,]    2    2    2    2
## [2,]    2    3    2    1
## [3,]    2    4    1    2
## [4,]    2    5    1    1
  1. use the rbind() and cbind() functions as a quick way to create small matrices
q <- cbind(c(2,3),c(4,5))
q
##      [,1] [,2]
## [1,]    2    4
## [2,]    3    5
  1. delete rows or columns by reassignment
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