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.
- An element at the mth row, nth column of A can be opened by the expression matrix [m, n].

r programming matrix

- Matrices are constructed as column-wise, so the inputs started in the "upper left" corner and running down the columns.
Read Also
Transpose MatrixSyntax:
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:

- 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.
- Here print () is used to print the value, that value stored in A variable.
Sample Output:

- Here in this output we display matrix with 3 rows and 3 columns.

- a[2, 3] specifies to elements at 2nd row and 3rd column
- 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]
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.