# R - Matrix

The matrix object in R is an array of two dimensions with the same class (data type).

## Constructor

You can create a matrix with three methods:

• the matrix method
• the columns and rows bindings methods
• the conversion of a vector in a matrix

### Matrix Function

````matrix(data = NA, ncol = 1, nrow = length(data)/ncol, byrow = FALSE, dimnames = NULL)`
```

where the attribute:

• data is an data vector (including a list or expression vector).
• ncol is the number of rows (default: 1)
• nrow is the number of rows (default: the length to fill the data in the matrix: length(data)/ncol)
• byrow describes how the matrix is filled (default by row)
• dimnames to describe the column and row headers of the matrix: NULL or a list of length 2 giving the row and column names respectively.

More:

````?matrix`
```

### (Columns of Rows) Vector Binding

``````# v1, ..., vn becomes columns of the matrix
cbind(v1, ..., vn)
# v1, ..., vn becomes rows of the matrix
rbind(v1, ..., vn) ```
```

where:

• v1 and vn are vectors

### Vector to matrix

With the following vector:

````> x`
```
```` 1 2 3 4 5 6`
```
````> is.vector(x)`
```
```` TRUE`
```

you can transform it as a matrix by applying dimensionality:

``````> dim(m)=c(2,3)
> m```
```
``````     [,1] [,2] [,3]
[1,]    1    2    3
[2,]   11   12   13```
```
``````# is.matrix returns TRUE if x is a vector and has a "dim" attribute of length 2 and FALSE otherwise.
> is.matrix(x)```
```
```` TRUE`
```

## Example

### Matrix Constructor

#### Without Data

````matrix(,3,3)`
```
``````     [,1] [,2] [,3]
[1,]   NA   NA   NA
[2,]   NA   NA   NA
[3,]   NA   NA   NA```
```

#### Byrow

• Matrix with Data filled by columns
````matrix(1:9,3,3,FALSE)`
```
``````     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9```
```
• Matrix with Data filled by rows
````matrix(1:9,3,3,TRUE)`
```
``````      [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9```
```

#### Dimnames

• Matrix with data and dimensions (row and column names)
``````> dimnames = list(c("Row1", "Row2"), c("Col1", "Col2", "Col3"))
> m <- matrix(c(1,11,2,12,3,13), nrow = 2, ncol = 3, dimnames = dimnames)```
```
``````     Col1 Col2 Col3
Row1    1    2    3
Row2   11   12   13```
```

#### dimensionality

##### default
• Matrix without dimensionality (default is 1 columns)
````matrix(1:6)`
```
``````     [,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4
[5,]    5
[6,]    6```
```
##### ncol

Matrix with only the number of columns

``````> m=matrix(1:6,ncol=2)
> m```
```
``````     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6```
```

### Columns and rows binding

With the following two vectors:

``````> v1=c(1,2,3)
> v2=c(4,5,6)```
```
• Matrix creation with Columns Binding
``````> m=cbind(v1,v2)
> m```
```
``````     v1 v2
[1,]  1  4
[2,]  2  5
[3,]  3  6```
```

* Matrix creation with Rows Binding

``````> m=rbind(v1,v2)
> m```
```
``````   [,1] [,2] [,3]
v1    1    2    3
v2    4    5    6```
```

## Operations

### Mathematical

``````s = seq (1,4)
m=matrix(s,2,2)
m```
```
``````     [,1] [,2]
[1,]    1    3
[2,]    2    4```
```
``````s2 = seq (4,1)
m2 = matrix(s2,2,2)
m2```
```
``````     [,1] [,2]
[1,]    4    2
[2,]    3    1```
```
````m*m2`
```
``````     [,1] [,2]
[1,]    4    6
[2,]    6    4```
```
````m-m2`
```
``````     [,1] [,2]
[1,]   -3    1
[2,]   -1    3```
```
````m/m2`
```
``````          [,1] [,2]
[1,] 0.2500000  1.5
[2,] 0.6666667  4.0```
```
````m^m2`
```
``````     [,1] [,2]
[1,]    1    9
[2,]    8    4```
```

## How to

### Check if it's a Matrix

````is.matrix(x)`
```

### Check the attributes

With the following matrix,

``````     Col1 Col2 Col3
Row1    1    2    3
Row2   11   12   13```
```

you can check the attributes with the attributes function.

````> attributes(m)`
```
``````\$dim
 2 3

\$dimnames
\$dimnames[]
 "Row1" "Row2"

\$dimnames[]
 "Col1" "Col2" "Col3"```
```

### Get the numbers of rows and/of columns

• Number of columns
````> ncol(m)`
```
```` 3`
```
• Number of rows
````> nrow(m)`
```
```` 2`
```

### Get the values of a cell

By indexing, you can retrieve values (by default as a vector).

````m=matrix(1:6,ncol=2)`
```
``````     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6```
```
• Indexing: Return a vector of the cell value located in the second row, first column:
````m[2,1]`
```
```` 2`
```
• Indexing without y coordinates. To return a vector of the second rows:
````m[2,]`
```
```` 2 5`
```
• Indexing (returning a matrix)
```` m[1, , drop = FALSE]`
```
``````     [,1] [,2]
[1,]    1    4```
```

### Change the dimensionality of a matrix

``````> m=matrix(1:6,ncol=2)
> m```
```
``````     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6```
```
``````> dim(m)=c(2,3)
> m```
```
``````     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6```
```