Matrix in R Language

Matrix is a rectangular arrangement of numbers in rows and columns. In a matrix, as we know rows are the ones that run horizontally, and columns are the ones that run vertically. In R programming, matrices are two-dimensional, homogeneous data structures. These are some examples of matrices: To create a matrix in R you need to use the function called matrix (). The arguments to this matrix () are the set of elements in the vector. You must pass how many numbers of rows and how many numbers of columns you want to have in your matrix.

Note: By default, matrices are in column-wise order.

Syntax to create Matrix:

We can create a matrix with the function matrix (). Following is a function to create a matrix in R which takes three arguments:

matrix (data, nrow, ncol, byrow = FALSE)

Arguments: 

  • data: The collection of elements that R will arrange into the rows and columns of the matrix \
  • nrow: Number of rows 
  • ncol: Number of columns 
  • byrow: The rows are filled from the left to the right. We use `byrow = FALSE` (default values), if we want the matrix to be filled by the columns i.e. the values are filled top to bottom