Created
February 11, 2016 07:33
-
-
Save aravindhebbali/ea5c75fd800071ba0f7a to your computer and use it in GitHub Desktop.
R Data Structures: Introduction to Matrices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# syntax | |
args(matrix) | |
# numeric matrix of 3 rows filled by columns | |
mat <- matrix(data = 1:9, nrow = 3, byrow = FALSE) | |
mat | |
# error in specifying rows/columns | |
mat1 <- matrix(data = 1:9, nrow = 2) | |
mat | |
mat <- matrix(data = 1:10, ncol = 3) | |
mat | |
# fill data by rows/columns | |
# matrix filled by rows | |
mat_row <- matrix(1:9, nrow = 3, byrow = TRUE) | |
mat_row | |
# matrix filled by columns | |
mat_col <- matrix(1:9, nrow = 3, byrow = FALSE) | |
mat_col | |
# number of rows/columns | |
# number of rows specified | |
mat1 <- matrix(1:9, nrow = 3) | |
mat1 | |
# number of columns specified | |
mat2 <- matrix(1:9, ncol = 3) | |
mat2 | |
# specify row/column names using list | |
# creating a simple list | |
# character vector | |
names <- c('John', 'Jovial', 'Jack') | |
# numeric vector | |
age <- c(25, 28, 24) | |
# create a list | |
details <- list(names, age) | |
details | |
# row names | |
row_names <- c('row1', 'row2', 'row3') | |
col_names <- c('col1', 'col2', 'col3') | |
# matrix with row/column names | |
mat_names <- matrix(data = 1:9, nrow = 3, dimnames = list(row_names, col_names)) | |
mat_names | |
# check dimension using dim function | |
# 3 x 3 matrix | |
mat1 <- matrix(1:9, nrow = 3) | |
# 3 x 4 matrix | |
mat2 <- matrix(1:12, nrow = 3) | |
# check dimension | |
dim(mat1) | |
dim(mat2) | |
# modify dimension using the dim function | |
# 3 x 4 matrix | |
mat1 <- matrix(1:12, nrow = 3) | |
mat1 | |
# change dimension to 4 x 3 | |
dim(mat1) <- c(4, 3) | |
mat1 | |
# 2 x 6 matrix | |
mat <- matrix(1:12, nrow = 2) | |
mat | |
# change dimension to 6 x 2 | |
dim(mat) <- c(6, 2) | |
mat | |
# coerce vectors | |
vect1 <- 1:10 | |
vect1 | |
dim(vect1) | |
# use dim to modify dimension | |
dim(vect1) <- c(2, 5) | |
vect1 | |
dim(vect1) | |
# as.matrix function | |
vect1 <- 1:10 | |
vect1 | |
# coerce to type matrix | |
as.matrix(vect1) | |
# matrix addition | |
mat1 <- matrix(1:9, nrow = 3) | |
mat2 <- matrix(sample(9), nrow = 3) | |
# add mat1 and mat2 | |
mat1 + mat2 | |
# add 5 to all elements of mat1 | |
mat1 + 5 | |
# add 3 to all elements of mat2 | |
mat2 + 3 | |
# matrix subtraction | |
mat1 | |
mat2 | |
# element wise matrix subtraction | |
mat1 - mat2 | |
# subtract 3 from all elements of mat1 | |
mat1 - 3 | |
# subtract 5 from all elements of mat2 | |
mat2 - 5 | |
# matrix division | |
mat1 | |
mat2 | |
# element wise matrix division | |
mat1 / mat2 | |
# divide all elements of mat1 by 3 | |
mat1 / 3 | |
# divide all elements of mat2 by 5 | |
mat2 / 5 | |
# transpose of a matrix | |
# 3 x 4 matrix | |
mat1 <- matrix(1:12, nrow = 3) | |
mat1 | |
# 4 x 3 matrix | |
t(mat1) | |
# matrix multiplication | |
# 3 x 4 matrix | |
mat1 <- matrix(1:12, nrow = 3) | |
mat1 | |
# 4 x 3 matrix | |
mat2 <- matrix(sample(12), nrow = 4) | |
mat2 | |
# use %*% to multiply matrices | |
mat1 %*% mat2 | |
# matrix inverse | |
# 3 x 3 matrix | |
mat1 <- matrix(sample(9), nrow = 3) | |
mat1 | |
# use solve() to compute the inverse | |
solve(mat1) | |
# append vector to columns | |
# 3 x 3 matrix | |
mat1 <- matrix(sample(9), nrow = 3) | |
mat1 | |
# numeric vector | |
vect1 <- sample(3) | |
vect1 | |
# append new row | |
rbind(mat1, vect1) | |
# append new column | |
cbind(mat1, vect1) | |
# combine matrices | |
mat1 <- matrix(1:9, nrow = 3) | |
mat1 | |
mat2 <- matrix(sample(9), ncol = 3) | |
mat2 | |
# combine by rows | |
rbind(mat1, mat2) | |
# combine by columns | |
cbind(mat1, mat2) | |
# index/subset matrices | |
# 4 x 3 matrix | |
mat <- matrix(1:12, nrow = 4) | |
mat | |
# subset element in first row/first column | |
mat[1, 1] | |
# subset element in second row/first column | |
mat[2, 1] | |
# subset element in fourth row/third column | |
mat[4, 3] | |
# 4 x 3 matrix | |
mat <- matrix(1:12, nrow = 4) | |
mat | |
# subset all rows of first column | |
mat[, 1] | |
# subset all columns of the second row | |
mat[2, ] | |
# subset using row/column names | |
# row names | |
row_names <- c('row1', 'row2', 'row3') | |
col_names <- c('col1', 'col2', 'col3') | |
# matrix with row/column names | |
mat_names <- matrix(data = 1:9, nrow = 3, dimnames = list(row_names, col_names)) | |
mat_names | |
# subset elements in first row/column | |
mat_names['row1', 'col1'] | |
# subset elements in second row | |
mat_names['row2', ] | |
# subset using logical expressions | |
# 4 x 3 matrix | |
mat <- matrix(1:12, nrow = 4) | |
mat | |
# elements greater than 4 | |
mat > 4 | |
# extract elements greater than 4 | |
mat[mat > 4] | |
# dissolve a matrix | |
# 3 x 3 matrix | |
mat <- matrix(1:9, nrow = 3) | |
mat | |
# use the c function | |
c(mat) | |
# use the as.vector function | |
as.vector(mat) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment