Also included information from video: https://youtu.be/p3EC-V9MiWU
Create matrix of integers to demonstrate the byrow and nrows arguments
Create a matrix containing 6 integers from of odd numbers with 2 and 3 columns Example: matrix_odd <- matrix(data = c(1,3,5,7,9,11), nrow = 2, ncol = 3, byrow=FALSE) The first arguemnt, (data = c(1,3,5,7,9,11) in this example) is the contents of the matrix The second and third arguments define the shape of the matrix, 2 rows and 3 columns in this example. The fourth argument byrow=TRUE or byrow=FALSE determines the order in which the integers will appear
If the number of components of the matrix is not evenly divisible by the number of rows, R will fill the rest of the matrix starting from the first component and give a warning message.
Example 1- How matrix looks when byrow=TRUE
matrix_odd <- matrix(data = c(1,3,5,7,9,11), nrow = 2, ncol = 3, byrow=TRUE)
matrix_odd
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 7 9 11
Example 2 - Shows what happens when byrow=FALSE
matrix_odd <- matrix(data = c(1,3,5,7,9,11), nrow = 2, ncol = 3, byrow=FALSE)
matrix_odd
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
Example 3 - Show what happens when the number of components in the matrix is not evenly divisible by the number of rows.
#Create a matrix of 8 digits with byrow=TRUE
#Number of rows (3) is not evenly divisible by number of digits (8)
matrix(1:8, byrow=TRUE, nrow = 3)
Warning: data length [8] is not a sub-multiple or multiple of the number of rows [3]
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 1
Create matrix from collection of vectors
Create vectors which must all be of the same data typeand combine them into a matrix
#Create nutrition vectors with data points for 5 weeks
nutrition_calories <- c(8926, 7768, 9043, 9406, 9640)
nutrition_carbs <- c(1257.6, 1183.5, 1315.3, 1393.5, 1491.7)
nutrition_protien <- c(470.7, 410.8, 416.9, 431.3, 386.1)
nutrition_fat <- c(218.5, 211.8, 282.1, 306.1, 270.3)
# Show contents of vectors
nutrition_calories
nutrition_carbs
nutrition_protien
nutrition_fat
#Combine vectors using the c() function
nutrients_5weeks <- c(nutrition_calories,nutrition_carbs,nutrition_protien,nutrition_fat)
#Create matrix from the combined vectors with a row for each of 5 weeks and the nutrients in columns
nutrients_matrix <-matrix(nutrients_5weeks, byrow=TRUE, nrow = 5)
#Display nutrients_matrix
nutrients_matrix
Name matrix rows
Name the rows of the matrix using rownames(matrix_name)
#Create a vector containing names for the rows
nutrition_weeks <- c("Week 1","Week 2","Week 3","Week 4","Week 5")
#Display nutrition_weeks
nutrition_weeks
#Name the rows of nutrients_matrix with the contents of the nutrition_weeks vector
rownames(nutrients_matrix) <- nutrition_weeks
#Display nutrients_matrix
nutrients_matrix
Name matrix columns Name the columns of the matrix using colnames(matrix_name)
#Create a vector containing names for the columns
nutrient_names <- c("Calories","Carbohydrates","Protein","Fat")
#Display nutrient_names
nutrient_names
#Name the rows of nutrients_matrix with the contents of the nutrition_weeks vector
colnames(nutrients_matrix) <- nutrient_names
#Display nutrients_matrix
nutrients_matrix
Create totals for matrix rows
Add a total column containing the sum of values in the rows of the matrix.
Calculate the total of each row of the matrix using rowsum() and store it in a vector. (The result isn’t useful for our nutrition example.)
#Calculate total nutrients for each week and store result in the week_total_nutrients vector
week_total_nutrients <- rowSums(nutrients_matrix)
#Display totals for rows
week_total_nutrients
Add the vector containing the row totals as a column to the matrix using the cbind() function. Example: new_matrix <- cbind(original_matrix, row_totals_vector)
#Combine nutrients matrix with vector containing row totals
nutrients_rowtot_matrix <- cbind(nutrients_matrix, week_total_nutrients)
#Display new matrix
nutrients_rowtot_matrix
Create totals for matrix columns
Add a total row containing the sum of values in the columns of the matrix.
Calculate the total of each column of the matrix using colsum() and store it in a vector.
#Calculate total of each nutrient for the 5-week period and store result in the week_total_nutrients vector
total_each_nutrient <- colSums(nutrients_matrix)
#Display totals for columns
total_each_nutrient
Add the vector containing the column totals as a row to the matrix using the rbind() function. Example: new_matrix <- rbind(original_matrix, cpl_totals_vector)
#Combine nutrients matrix with vector containing row totals
nutrients_coltot_matrix <- rbind(nutrients_matrix, total_each_nutrient)
#Display new matrix
nutrients_coltot_matrix
Selecting elements from a matrix
Must use square brackets, not parentheses.
Select entire column - When there is no number preceding the comma, all rows are selected for the column indicated by the number following the comma. Example - new_vector <- matrix_name[,3] selects all rows for the 3rd column of the matrix.
#Select column from a matrix and assign it to a vector
#Display nutrients_coltot_matrix
nutrients_coltot_matrix
#Select 1st column of matrix (Calories)
calories_5wks_vector <- nutrients_coltot_matrix[,1]
#Display 1st column
calories_5wks_vector
Select sequential set of rows from a particular column To select a sequential set of rows use a colon between the numbers of the desired rows Example - new_vector <- matrix_name[4:5,2] selects the 4th & 5th rows for the 2nd column of the matrix.
#Select rows 4 & 5 from the 2nd column of a matrix (Calories for weeks 4 & 5)
calories_wks_4_5 <- nutrients_matrix[4:5,2]
#Display result
calories_wks_4_5
Select entire row - When there is no number following the comma, all columns are selected for the row indicated by the number preceding the comma.
#Select row from a matrix and assign it to a vector
#Display nutrients_coltot_matrix
nutrients_rowtot_matrix
week2_vector <- nutrients_rowtot_matrix[2,]
#Display 2nd row
week2_vector
Select sequential rows - To select several rows, put a column between the numbers of the desired rows. Example - new_vector <- matrix_name[3:5,] selects the 3rd, 4th & 5th rows for all columns of the matrix.
#Select rows 3-5 from the matrix (All nutrients for weeks 3-5)
all_nutrients_wks_3_5 <- nutrients_matrix[3:5,]
#Display result
all_nutrients_wks_3_5
Arithmetic With Matrices
Add all nutrients from week 1 and week 2 from nutrients_matrix
#Create 2 matrices to sum
nutrients_wks_1_2 <- nutrients_matrix[1:2,]
nutrients_wks_3_4 <- nutrients_matrix[3:4,]
#Display 2 new matrices
nutrients_wks_1_2
nutrients_wks_3_4
#Add matrices to create nutrients_wks_1_4
nutrients_wks_1_4 <- nutrients_wks_1_2 + nutrients_wks_3_4
#Display result of addition
nutrients_wks_1_4
Arithmetic With Matrices
Example 1: Add 100 to elements of a matrix and assigning a name to the resulting matrix. Another option is to just do the calculation and the console will display the result. The example shows adding 200 to the original matrix.
200 + matrix_odd
[,1] [,2] [,3]
[1,] 201 205 209
[2,] 203 207 211
Example 2 - Multiply two matrices of the same size and shape.
#Create two matrices
matrix_odd <- matrix(data = c(1,3,5,7), nrow = 2, ncol = 2, byrow=TRUE)
matrix_even <- matrix(data = c(2,4,6,8), nrow = 2, ncol = 2, byrow=TRUE)
#Multiply mattrices
matrix_odd
[,1] [,2]
[1,] 1 3
[2,] 5 7
matrix_even
[,1] [,2]
[1,] 2 4
[2,] 6 8
matrix_odd * matrix_even
[,1] [,2]
[1,] 2 12
[2,] 30 56