Recreate Professor’s Act #6 to your own

Vector Elemet 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.

#creates a vector of 3 even numbers fror x1
#names - 'character vector type'
#'NULL'- is returned because the object has no names
x1 <- c(2,4,6)
names(x1)
## NULL

We can assign or query vector element names via the names() function:

#Names()- shown now because letters are being used (characters) not integers like the last one.
names(x1) <- c("x","y","xy")
names(x1)
## [1] "x"  "y"  "xy"
x1
##  x  y xy 
##  2  4  6

We can remove the names from a vector by assigning NULL:

#x1 has 2 elements attached to it both Int and Character names. 
#NULL- allows you to null out the character names portion
names(x1) <- NULL
x1
## [1] 2 4 6

We can even reference elements of the vector by name:

#You are able to refernce the column elements by searching for 1 element. 4 is in the middle and so is Y, hence why they both appear when y is being asked.
x1 <- c(2,4,6)
names(x1) <- c("x","y","xy")
x1["y"]
## y 
## 4

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

#A matrix dataset is created, and named 'a', in which 2:8 even #'s are created in a 2:2 dataframe. 
a<-matrix(c(2,4,6,8),nrow=2,ncol=2)
a
##      [,1] [,2]
## [1,]    2    6
## [2,]    4    8

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:

#ncol- is not needed because there are only 4 elements, so they themselves would create a 2:2. nrow() is enough.
a<-matrix(c(2,4,6,8),nrow=2)
a
##      [,1] [,2]
## [1,]    2    6
## [2,]    4    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:

#[row,column]
# from the 'a' matrix get all the row elements for column 2
a[,2]
## [1] 6 8

Another way to build y is to specify elements individually:

#A 2:2 matrix is being created, calld 'a'. in which there are as of yet no elements. That is why 'NA' IS DISPLAYED as the values.
a <- matrix(nrow=2,ncol=2)
a
##      [,1] [,2]
## [1,]   NA   NA
## [2,]   NA   NA
#Creating own matrix individully. 
a[1,1] <- 2
a[2,1] <- 4
a[1,2] <- 6
a[2,2] <- 8
a
##      [,1] [,2]
## [1,]    2    6
## [2,]    4    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:

#byrow=TRUE indicates that the matrix should be filled by rows.
b <- matrix(c(1,2,3,4,5,6),nrow=2,byrow=T)
b
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6

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:

a %*% a # mathematical matrix multiplication
##      [,1] [,2]
## [1,]   28   60
## [2,]   40   88
4*a # mathematical multiplication of matrix by scalar
##      [,1] [,2]
## [1,]    8   24
## [2,]   16   32
a+a # mathematical matrix additio
##      [,1] [,2]
## [1,]    4   12
## [2,]    8   16

Matrix Indexing

Let us say that we have a matrix z1:

#matrix created Individually with even numbers
z1 <- matrix(nrow=4,ncol=3)
z1[1,1] <- 2
z1[2,1] <- 4
z1[1,2] <- 6
z1[2,2] <- 8
z1[3,1] <- 10
z1[3,2] <- 12
z1[3,3] <- 14
z1[1,3] <- 16
z1[2,3] <- 0
z1[4,1] <- 2
z1[4,2] <- 4
z1[4,3] <- 6
z1
##      [,1] [,2] [,3]
## [1,]    2    6   16
## [2,]    4    8    0
## [3,]   10   12   14
## [4,]    2    4    6

Let us obtain the submatrix of z1 consisting of all elements with column numbers 1 and 2 and any row number. This extracts the first and second columns.

#1st and 2nd columns obtained from z1
z1[,1:2]
##      [,1] [,2]
## [1,]    2    6
## [2,]    4    8
## [3,]   10   12
## [4,]    2    4
#referencing the a matrix created
a
##      [,1] [,2]
## [1,]    2    6
## [2,]    4    8
#calling for the 1row, all columns, from the a matrix
a[1,]
## [1] 2 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

#creating a vector called x1, with square rooted numbers
x1<-c(9,16,25,36,49,64)
x1
## [1]  9 16 25 36 49 64
#adding the number '81' to the end of the vector of 'x1'
x1 <- c(x1,81)
x1
## [1]  9 16 25 36 49 64 81
#referencing the 1st 3 elements of x1, then adding a '1' to the vector, then calling the next 4:6 elements.
x1 <- c(x1[1:3], 1, x1[4:6])
x1
## [1]  9 16 25  1 36 49 64
x1 <- x1[-2:-4] # delete elements 2 through 4
x1
## [1]  9 36 49 64

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.

#creating a vector called 'two'
two <- c(2,2,2,2)
two
## [1] 2 2 2 2
z1
##      [,1] [,2] [,3]
## [1,]    2    6   16
## [2,]    4    8    0
## [3,]   10   12   14
## [4,]    2    4    6
#binding the vector two to the beginning of z1.
cbind(two,z1)
##      two         
## [1,]   2  2  6 16
## [2,]   2  4  8  0
## [3,]   2 10 12 14
## [4,]   2  2  4  6

Here, cbind() creates a new matrix by combining a column of 2s with the columns of z1. We choose to get a quick printout, but we could have assigned the result to z1 (or another variable), as follows:

cbind(z1,two)
##               two
## [1,]  2  6 16   2
## [2,]  4  8  0   2
## [3,] 10 12 14   2
## [4,]  2  4  6   2

Note, too, that we could have relied on recycling:

#used 1 in instead of 'two' for the 1st column of the new matrix
cbind(1,z1)
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    6   16
## [2,]    1    4    8    0
## [3,]    1   10   12   14
## [4,]    1    2    4    6

You can also use the rbind() and cbind() functions as a quick way to create small matrices. Here’s an example:

#quick matrix created using the 'cbind' function.
q1 <- cbind(c(2,4),c(6,8))
q1
##      [,1] [,2]
## [1,]    2    6
## [2,]    4    8

You can delete rows or columns by reassignment, too:

b <- matrix(1:6,nrow=3)
b
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
#reassigned b to only have its previous 1,3 row and all the columns, different form of deleting a set.
b <- b[c(1,3),]
b
##      [,1] [,2]
## [1,]    1    4
## [2,]    3    6