Press "Enter" to skip to content

What is a Matrix in R?

Zigya Acadmey 0

In R, a matrix is a collection of elements of the same data type (numeric, character, or logical) arranged into a fixed number of rows and columns.

Creating Matrices

One of the easiest ways to create a matrix is with the matrix() function.

The basic syntax for creating a matrix in R is

matrix(data, nrow, ncol, byrow, dimnames)
Arguments

data: An optional data vector (including a list or expression vector). Non-atomic classed R objects are coerced by as.vector and all attributes discarded.

nrow: The desired number of rows.

ncolt : He desired number of columns.

byrow: Logical. If FALSE (the default) the matrix is filled by columns, otherwise, the matrix is filled by rows.

dimnames: dimnames attribute for the matrix: NULL or a list of length 2 giving the row and column names respectively. An empty list is treated as NULL, and a list of length one as row names, and the names in the list will be used by the dimension rows and columns.

> A = matrix( 
+   c(10, 20, 30, 40, 50, 60), # the data elements 
+   nrow=2,              # number of rows 
+   ncol=3,              # number of columns 
+   byrow = TRUE)        # fill matrix by rows 

     [,1] [,2] [,3]
[1,]   10   20   30
[2,]   40   50   60

Note: the byrow=TRUE means that we will the matrix by the row, it is not the same as if we do not fill it by row:

We can also create matrices purely by expressing the number of columns we wish to have. In larger forms of data we may not know the exact amount of rows and columns but certainly we can choose at least the number of columns.

> y <- c(1,2, 3, 4, 5,6, 7, 8, 9, 0)
> x <- matrix(y, nrow=2)
> x

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8    0

Accessing Elements of a Matrix

In fact, we can access the elements of a matrix with the use of column and row index. We consider the matrix x above to find the specific elements below.

# To access 2nd row and 3rd col of matrix
> x[2,3]
[1] 6

# To access 1st row and 4th col of matrix
> x[1,4]
[1] 7

Operation on two Matrix

Moreover, we can perform various mathematical operations on the matrices using the R operators. Thus the results of the operation are also a matrix.

To perform any operation on a matrix the dimension of both the matrix must be same (columns and rows).

# Create a first matrix
> mat1 <- matrix(c(1, 2, 3, 4), nrow=2, byrow=TRUE)
> mat1

     [,1] [,2]
[1,]    1    3
[2,]    2    4

# Create a second matrix
> mat2 <- matrix(c(2, 0, 0, 2), nrow=2)
> mat2

     [,1] [,2]
[1,]    2    0
[2,]    0    2

# Adding two matrix
> mat3 <- mat1 + mat2
> mat3

     [,1] [,2]
[1,]    3    2
[2,]    3    6

# Multiplying two matrix
> mat4 <- mat1 * mat2
> mat4

     [,1] [,2]
[1,]    2    0
[2,]    0    8

Conclusion

In this article, we learned about Matrix, What is a matrix, How to create one, What type of arguments are allowed in matrix() function, and how to access an element of the matrix along with performing basic operations.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published. Required fields are marked *