Working with matrices Flashcards
Assign name attributes to rows of a matrix
rownames(matrix)
Assign name attributes to columns of a matrix
colnames(matrix)
Finding matrix dimensions
dim(math_chemistry)
Combining vectors or matrices by row
rbind(matrix_1, matrix_2)
rbind(vector_1, vector_2)
rbind(vector_1, matrix_1)
Combining vectors or matrices by column
cbind(matrix_1, matrix_2)
cbind(vector_1, vector_2)
cbind(vector_1, matrix_1)
Extract a single element
matrix[2, 5]
matrix[“row”, “column”]
Extract multiple elements
matrix[c(1, 2), c(1, 3)]
matrix[c(“Harvard”, “Stanford”), c(“world_rank, “influence”)]
Extract a single row
matrix[1, ]
matrix[“Harvard”, ]
Extract a single column
matrix[ , 2]
matrix[ , “quality_of_education”]
Extract multiple rows or columns
matrix[c(2, 4, 5), ]
matrix[ , c(“quality”, “influence”, impact”)]
Sum of values in a matrix
sum(matrix[ , “column_name”])
sum(matrix[“row_name”, ])