R Programming - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

R Matrices

R Matrices

shape Description

Matrixes are similar to data frames as matrix also have two dimensional arrangement, just like a spreadsheet. However, unlike data frames, matrixes are homogeneous data structures. Means, a matrix can contain items of similar type only. Typically, generally use matrixes to store and process numeric data.The following is the syntax for the matrices. [c]matrix(data, nrow, ncol, byrow, dimnames)[/c]

shape Example

The matrices are similar to data frames. The Data Frame is arguably the most popular data structure in R. It is a heterogeneous data structure. Means this data structure can also contain elements of different classes, just like the list. However, unlike list, data frame is 2-dimensional in nature. The following is an example to create a matrix. [c]# Elements are arranged sequentially by row. M <- matrix(c(2:13), nrow = 4, byrow = TRUE) print(M) # Elements are arranged sequentially by column. N <- matrix(c(2:13), nrow = 4, byrow = FALSE) print(N) # Define the column and row names. rownames = c("row1", "row2", "row3", "row4") colnames = c("col1", "col2", "col3") P <- matrix(c(2:13), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames)) print(P)[/c] In the above example, four rows have been created with row names and columns names, now compile the code result will be as follows. [c]> [,1] [,2] [,3] [1,] 2 3 4 [2,] 5 6 7 [3,] 8 9 10 [4,] 11 12 13 [,1] [,2] [,3] [1,] 2 6 10 [2,] 3 7 11 [3,] 4 8 12 [4,] 5 9 13 col1 col2 col3 row1 2 3 4 row2 5 6 7 row3 8 9 10 row4 11 12 13[/c]

Ar-thematic operations on matrices

The following is an example to perform addition, subtraction etc on the matrices. [c]# Create two 2x3 matrices. matrix1 <- matrix(c(5, 7, -1, 5, 4, 9), nrow = 2) print(matrix1) matrix2 <- matrix(c(12, 3, 0, 5, 7, 9), nrow = 2) print(matrix2) # Add the matrices. result <- matrix1 + matrix2 cat("Result of addition","\n") print(result) # Subtract the matrices result <- matrix1 - matrix2 cat("Result of subtraction","\n") print(result)[/c] In the above example, 2x3 matrices has been created with some values, when addition and subtraction on those values the result will be as follows. [c]> print(matrix1) [,1] [,2] [,3] [1,] 5 -1 4 [2,] 7 5 9 > > matrix2 <- matrix(c(12, 3, 0, 5, 7, 9), nrow = 2) > print(matrix2) [,1] [,2] [,3] [1,] 12 0 7 [2,] 3 5 9 > > # Add the matrices. > result <- matrix1 + matrix2 > cat("Result of addition","\n") Result of addition > print(result) [,1] [,2] [,3] [1,] 17 -1 11 [2,] 10 10 18 > > # Subtract the matrices > result <- matrix1 - matrix2 > cat("Result of subtraction","\n") Result of subtraction > print(result) [,1] [,2] [,3] [1,] -7 -1 -3 [2,] 4 0 0[/c] As like user can perform division and multiplication also. [c]# Create two 2x3 matrices. matrix1 <- matrix(c(5, 7, -1, 5, 4, 9), nrow = 2) print(matrix1) matrix2 <- matrix(c(12, 3, 0, 5, 7, 9), nrow = 2) print(matrix2) # Multyply the matrices. result <- matrix1 * matrix2 cat("Result of multiplication","\n") print(result) # Divide the matrices result <- matrix1/matrix2 cat("Result of division","\n") print(result)[/c] Output: The result will be as follows. [c]> print(matrix1) [,1] [,2] [,3] [1,] 5 -1 4 [2,] 7 5 9 > > matrix2 <- matrix(c(12, 3, 0, 5, 7, 9), nrow = 2) > print(matrix2) [,1] [,2] [,3] [1,] 12 0 7 [2,] 3 5 9 > > # Multyply the matrices. > result <- matrix1 * matrix2 > cat("Result of multiplication","\n") Result of multiplication > print(result) [,1] [,2] [,3] [1,] 60 0 28 [2,] 21 25 81 > > # Divide the matrices > result <- matrix1/matrix2 > cat("Result of division","\n") Result of division > print(result) [,1] [,2] [,3] [1,] 0.4166667 -Inf 0.5714286 [2,] 2.3333333 1 1.0000000[/c]

Summary

shape Key Points

  • The matrix() is the function to create the matrix type.
  • Matrixes are similar to data frames as matrix also have two dimensional arrangement
  • The matrixes are homogeneous data structures.