Matrices

Matrices

A matrix is a two-dimensional object. In other words, we can store data in a matrix in both rows and columns.

> my_matrix

//Output Below

     [,1]    [,2]    [,3]
[1,]    1      3      5
[2,]    2      4      6

See that? We just printed out a matrix with 2 rows and 3 columns.

Creating a matrix

We use the matrix() function to create a matrix from vectors. Similar to vectors, a matrix can only contain one type of value. 

Let's create a matrix that has only two rows.

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

//Output Below

     [,1]   [,2]   [,3]
[1,]    1      3      5
[2,]    2      4      6

Awesome! To create a matrix we need to provide a vector and specify the number of rows and columns.

Creating a matrix II

Now let's make a matrix that only has two columns.

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

//Output Below

     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

Whop whop! Creating matrices is easy in R. Let's see what else we can find out about matrices.

Creating by row

We can fill our matrices in two ways: by rows or by columns. To do so, we just need to add the byrow attribute and set it to TRUE or FALSE.

> data <- 1:6
> my_matrix <- matrix(data,nrow=2,ncol=3,byrow=TRUE)
> my_matrix

//Output Below

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

Sweet! If byrow=TRUE, the first row is filled, followed by the second.

By column

Let's figure out how to add the values by column.

> data <- 1:6
> my_matrix <- matrix(data,nrow=2,ncol=3,byrow=FALSE)
> my_matrix

//Output Below

      [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

Wohoo! When byrow is FALSE, the first column is filled, followed by the second and so on.

Column names

We can also rename the columns of our matrix using the colnames() function.

> my_matrix <- matrix(1:6,3)
> colnames(my_matrix) <- c("run","cycle")
> my_matrix

//Output Below

     run cycle
[1,]   1     4
[2,]   2     5
[3,]   3     6

Amazing! Notice how we don't have to type nrow = 3. The second variable in matrix() is automatically treated as the number of rows.

Row names

We can rename the rows of the matrix in a similar fashion using the rownames() function.

> my_matrix <- matrix(1:6, 3) 
> rownames(my_matrix) <- c("Anne","Luke","Emma")
> my_matrix

//Output Below

     run cycle
Anne   1     4
Luke   2     5
Emma   3     6

Nice! We can now see who's a better runner or biker.

Row sums

Let's calculate the total distance per person by computing a sum of each row using our previous matrix.

> my_matrix
> rowSums(my_matrix)

//Output Below

     run cycle
Anne   1     4
Luke   2     5
Emma   3     6

Anne Luke Emma 
   5    7    9 

Awesome! rowSums() returns a vector containing sums for each row in the matrix.

Column sums

We can now try computing the distance covered by all individuals per activity.

> my_matrix
> colSums(my_matrix)

//Output Below

     run cycle
Anne   1     4
Luke   2     5
Emma   3     6

[1] 6 15

Nice! colSums() returns a vector containing sums for each column in the matrix.

Subsetting

We subset matrices by typing the row and then the column indices inside [].

Let's try to get the 3rd row and second column element from my_matrix.

> my_matrix
> my_matrix[3,2]

//Output Below

     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

[1] 6

Nice! As with vectors, we subset matrices using brackets in the following format: [row_index , column_index].

Adding a row

If we want to add a row to our matrix, we can do so by simply using the rbind() function.

> rbind(my_matrix, c(7,8))
> my_matrix

//Output Below

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

Nice! Remember that the row being added must have the same length as the number of columns in the matrix.

Adding a column

To add a column, we need to use the cbind() function. We also have to keep a careful eye on the length of the added column.

> my_matrix <- matrix(1:6, 3)
> cbind(my_matrix, c(9, 8, 7))

//Output Below

     [,1] [,2] [,3]
[1,]    1    4    9
[2,]    2    5    8
[3,]    3    6    7

Awesome! The column we are adding should have the same length as the number of rows in our matrix.

Matrix Arithmetics

Dividing or multiplying a matrix with a constant will divide or multiply every matrix element with that constant.

Let's see what happens when we divide a matrix by 2.

> my_matrix <- matrix(1:6, 3)
> my_matrix / 2

//Output Below

     [,1] [,2]
[1,]  0.5  2.0
[2,]  1.0  2.5
[3,]  1.5  3.0

Sweet! *, +, and - work the same way. The operation is carried out between the constant and each individual element.

More Arithmetics

Doing arithmetic calculation with two matrices of the same size will compute the corresponding matrix elements.

> my_matrix <- matrix(1:6, 3)
> my_matrix + my_matrix

//Output Below

     [,1] [,2]
[1,]    2    8
[2,]    4   10
[3,]    6   12

Great! The arithmetic operation is performed between each corresponding element.