# Basics Calculations: Matrix Operations in R Language

In R, a 4 𝗑 2-matrix X can be created with a following command:

> x <-  matrix (nrow=4,   ncol=2,  data=c(1,2,3,4,5,6,7,8)  )

> x
[,1]       [,2]
[1,]             1          5
[2,]             2          6
[3,]             3          7
[4,]             4          8

Properties of a Matrix

We can get specific properties of a matrix: > dim (x)         # tells the
   4   2             dimension of matrix

> nrow (x)       # tells
  4                    the number of rows

> ncol (x)        # tells
  2                  the number of columns

> mode (x)      # Informs the type or storage mode of an object, e.g., numerical, logical etc.
   "numeric"
attributes provides all the attributes of an object

> attributes (x)    # Informs the dimension of matrix
\$dim       4   2

Help on the Object "Matrix"

To know more about these important objects, we use R-help on "matrix".
> help ("matrix")
matrix     package:base            R Documentation
Matrices
Description :
'matrix'  creates a matrix from the given set of values.
'as.matrix' attempts to turn its argument into a matrix.
'is.matrix'  tests if its argument is a (strict) matrix. It is generic: you can write methods to handle specific classes of objects, see Internal Methods.

Then we get an overview on how a matrix can be created and what parameters are available:

Usage :
matrix(data  [= NA, nrow = 1 , ncol = 1, byrow = FALSE, dimension = NULL)
as.matrix (x)
is. matrix (x)

Arguments :
data: an optional data vector.
nrow: the desired number of rows
ncol: the desired number of columns
byrow: logical. If 'FALSE' (the default) the matrix is filled by columns, otherwise the matrix is filled by rows.

dimnames:  A 'dimnames'  attribute for the matrix: a 'list' of length 2.
x: an R object.

Finally, references and cross-references are displayed...
References :
Becker, R. A.,  Chambers, J. M. and wilks, A.
R. (1988)  _The New S Language_. wadsworth & Books/Cole.

'data.matrix' , which attempts to convert to a numeric matrix.
.... as well as an example:

Examples :
is.matrix (as.matrix (1 : 10) )
data (warpbreaks)
! is.matrix(warpbreaks) #  data.frame, NOT matrix!
warpbreaks [1 : 10,]
as.matrix(warpbreaks[1 : 10,])  #using
as.matrix.data.frame(.) method

Matrix Operations

Assigning a specified number to all matrix elements:

> x  <-  matrix (nrow=4, ncol=2, data=2 )
> x
[,1]    [,2]
[1,]         2        2
[2,]         2        2
[3,]         2        2
[4,]         2        2

Construction of a diagonal matrix, here the identity matrix of a dimension 2:

> d  <-  diag (1,  nrow=2,  ncol=2)
> d
[,1]   [,2]
[1,]    1       0
[2,]    0       1

Transpose of a matrix x:  x'

>  x  <- matrix (nrow=4, ncol=2, data=1:8,  byrow=T )
>  x
[,1]      [,2]
[1,]             1          2
[2,]             3          4
[3,]             5          6
[4,]             7          8

Multiplication of a matrix with a constant  