R Data Matrix | R Datatypes Matrices - r - learn r - r programming

  • In R datatypes, Matrices are a special vector with dimensional attributes.
  • These attributes represent the rows and columns.
  • A matrix is a two-dimensional rectangular data set.
  • It used two integer vector inputs to form a matrix function.
  • r programming matrix

    r programming matrix

  • An element at the mth row, nth column of A can be opened by the expression matrix [m, n].
 r matrix image
  • Matrices are constructed as column-wise, so the inputs started in the "upper left" corner and running down the columns.


Matrix(data = NA, nrow = 1, ncol = 1, byrow = TRUE or FALSE)
  • Data: data is an optional vector.
  • nrow: nrow is specifies to number of rows.
  • ncol: ncol is specifies to number of columns.
  • byrow: by row is a logical vector. If TRUE (the default) the matrix is filled by rows, otherwise the matrix is filled by columns.

Sample Code:

A = matrix( c('0','1','2','3','4','5','6','7','8'), nrow = 3, ncol = 3, byrow = TRUE)

Code Explanation:

 r datatypes matrices structure
  1. In this example, A is specifying to string variable. Here ('0','1','2','3','4','5','6','7','8') is data elements. nrow =3 is represents number of rows is 3, ncol =3 declare as number of columns is 3, and TRUE (the default) the matrix is filled by rows.
  2. Here print () is used to print the value, that value stored in A variable.

Sample Output:

 r datatypes matrices output
  1. Here in this output we display matrix with 3 rows and 3 columns.
 r datatypes matrices structure output
  1. a[2, 3] specifies to elements at 2nd row and 3rd column
  2. 5 is selected element of 2nd row and 3rd column.

Matrix Manupulation

  • We also used the rownames() and colnames() functions to set the row and column names.
> colnames(XY)
> rownames(XY) <- LETTERS[1:5]
  • Matrices are also created by binding two vectors of equal length.
  • If it is created by column-wise usind cbind() or created by row-wise using rbind() functions to combine them into a single matrix.
cbind(X, Y)
rbind(X, Y)
  • prod() function returns the multiplication results of all the values present in its arguments.
  • svd() function computes the singular-value decomposition of a rectangular matrix.
  • dim() function gets or sets the dimension of a matrix.

Related Searches to R Data Matrix | R Datatypes Matrices