# Matrices - Part 2

## Introduction

In the previous post, we learnt to create matrices. In this post, we will learn to:

• combining matrices
• index/subset matrices
• dissolve matrices

## Append Data

In this section, we will learn how to append data to a matrix. There are two functions that can be used for this purpose:

• `rbind()`
• `cbind()`

`cbind` will append a new column to the matrix while `rbind` will append a new row.

#### Append Row/Column

``````# 3 x 3 matrix
mat <- matrix(data = 1:9, nrow = 3)
mat``````
``````##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9``````
``````# numeric vector
v <- c(10, 11, 12)
v``````
``##  10 11 12``
``````# append row
rbind(mat, v)``````
``````##   [,1] [,2] [,3]
##      1    4    7
##      2    5    8
##      3    6    9
## v   10   11   12``````
``````# append column
cbind(mat, v)``````
``````##             v
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12``````

## Combine Matrices

When you use `rbind` to combine two matrices, the number of columns must match and in case of `cbind`, the number of rows must match.

#### Append Row/Column

``````# 3 x 3 matrix
mat1 <- matrix(data = 1:9, nrow = 3)
mat2 <- matrix(data = sample(9), nrow = 3)

# append rows
rbind(mat1, mat2)``````
``````##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
## [4,]    2    4    7
## [5,]    5    3    8
## [6,]    9    6    1``````
``````# append columns
cbind(mat1, mat2)``````
``````##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    4    7    2    4    7
## [2,]    2    5    8    5    3    8
## [3,]    3    6    9    9    6    1``````

## Subset Matrices

In this section, we will learn to subset matrices. The `[]` operator can be used to subset matrices just like vectors but since matrices are two dimensional, we need to specify both the row number and the column number. Below are a few examples:

``````# 3 x 4 matrix
mat <- matrix(data = 1:12, nrow =3)
mat``````
``````##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12``````
``````# extract element from first row, first column
mat[1, 1]``````
``##  1``
``````# extract all rows of first column
mat[, 1]``````
``##  1 2 3``
``````# extract all columns of first row
mat[1,]``````
``##   1  4  7 10``
``````# extract 2nd and 3rd row of first column
mat[c(2, 3), 1]``````
``##  2 3``
``````# extract 2nd and 3rd column of first row
mat[1, c(2, 3)]``````
``##  4 7``
``````# extract 2nd and 3rd row of first and third column
mat[c(2, 3), c(1, 3)]``````
``````##      [,1] [,2]
## [1,]    2    8
## [2,]    3    9``````

#### Using Row & Column Names

In an earlier section, we learnt how to name the rows and columns of a matrix. Let us see how these names can be used to subset matrices.

``````# row names
row_names <- c('row_1', 'row_2', 'row_3')

# column names
col_names <- c('col_1', 'col_2', 'col_3')

# matrix with row and column names
mat <- matrix(data = 1:9, nrow = 3, dimnames = list(row_names, col_names))

# extract elements from first row/columns
mat['row_1', 'col_1']``````
``##  1``
``````# extract all rows of first column
mat[, 'col_1']``````
``````## row_1 row_2 row_3
##     1     2     3``````
``````# extract all columns of first row
mat['row_1',]``````
``````## col_1 col_2 col_3
##     1     4     7``````

#### Using Logical Expressions

We can use logical expressions to subset matrices.

``````# 3 x 4 matrix
mat <- matrix(data = 1:12, nrow =3)
mat``````
``````##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12``````
``````# elements greater than 4
mat > 4``````
``````##       [,1]  [,2] [,3] [,4]
## [1,] FALSE FALSE TRUE TRUE
## [2,] FALSE  TRUE TRUE TRUE
## [3,] FALSE  TRUE TRUE TRUE``````
``````# extract elements greater than 4
mat[mat > 4]``````
``##   5  6  7  8  9 10 11 12``

## Dissolve Matrices

Till now we have learnt how to coerce a vector into matrix. Now let us learn how to coerce a matrix into a vector using:

• `c()`
• `as.vector()`
``````# 3 x 3 matrix
mat <- matrix(data = 1:9, nrow =3)
mat``````
``````##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9``````
``````# using c()
c(mat)``````
``##  1 2 3 4 5 6 7 8 9``
``````# using as.vector()
as.vector(mat)``````
``##  1 2 3 4 5 6 7 8 9``