Working With Matrices

A matrix is a collection of elements of the same data type, with the data being arranged into rows and columns. Because it consists of both rows and columns, matrices are considered two-dimensional as opposed to vectors, which are considered one-dimensional. 

DataQuest analyzes university rankings for this lesson. However, for this post, I decided that I would analyze six of the highest-grossing films of all time. The data that I’ll be working with in this post comes from Box Office Mojo. Please note that for the last two columns, Budget in Millions and Domestic Opening in Millions, I rounded the numbers so the values in these columns are not exact.

Combining Vectors into Matrices

To create a matrix using the data above, DataQuest taught me that I must first create vectors.

Creation of vectors in R.

I can easily combine this vectors into a matrix using the function rbind(). The r in rbind() stands for row and this function allows us to combine multiple vectors and matrices by row.

Combining rows in R.

Naming Matrix Rows and Columns

I then learned that I could name the rows and columns in a matrix. I could use the functions rownames() to name rows and colnames() to name columns. First, I stored the names of the columns into a vector called categories. I then used the function colnames() to assign those names to the columns in my matrix.

Naming column rows in R.

Finding Matrix Dimensions

If I wanted to identify the dimensions (the number of rows and columns) in a matrix, I would use the dim() function. The output of this function gives me two numbers. The first number is the number of rows; the second number is the number of columns.

Finding the dimensions of a matrix in R.

Adding Columns to Matrices

Earlier in this post, I combined vectors into a matrix using rbind() and it allowed me to combine my vectors by row. The function, cbind() allows me to combine vectors and matrices by column.

Let’s say I wanted to add the domestic gross of the films as a column to this matrix. First, I would get the domestic gross of the films. Next, I would use cbind() to add the domestic_gross_millions column to the existing matrix.

adding a column to a matrix.

I then stored the result in a new matrix called entire_matrix.

adding a column to a matrix and storing result to a new matrix.

When adding a vector to a matrix, it’s important to make sure that the new vector is the same length as the number of rows and columns in the matrix.

Indexing Matrices

Just as I indexed vectors, I learned that I could also index matrices. Since matrices are two-dimensional, they can be indexed in the following ways:    

  • index to select specific values       
  • index to select specific rows and columns

Indexing By Element

Let’s say I wanted to extract the year that Avengers: Infinity War was released. I have to specify the location of this element by row and and column. In the screenshot below, you can see that Infinity War is in row 5 and the year is in column 2.

Describing location of element in row 5 and column 2.
Indexing by position

I can also index matrices by row and column names instead of position:

Indexing by row and column name.

I can specify the range of columns since the budget_in_millions and domestic_gross_millions columns are next to each other.

Indexing matrix by specifying range of columns

I can also index columns are not next to each other. Let’s say I wanted index elements from the columns rank and runtime_minutes. Here I index these columns in two ways. The first example is by position, the second example is by name.

Index Multiple elements by position.
Index multiple elements by name

Index By Row and Column

As mentioned, I can index to select a specific row or column. Let’s say I want to extract all the rankings for Avatar. All the rankings for Avatar are in row 2 of my matrix. I would indicate that I want to index all the elements of row 2 and leave the column position blank.

Index entire row.

When I write an expression to index an entire row or column, I only need to specify the name of that row or column. The other position is left blank. In this next example, I index an entire column. Since row comes before column, I leave the row blank.

Index entire column

I could also index to select multiple rows and columns. If I want to extract the year, runtime_minutes and budget_millions columns, I would write:

Extract column by name
Extract column by position.

If I want to extract the star_wars, infinity_war and jurassic_world rows, I would write:

Extract row by name
Extract row by position

Ranking Films

I can use the rank() function to specify the categories I want to rank the films by. This function returns a vector of numeric values.

Using the rank function to specify the film categories to rank.

Calculating the Sum Of Values in A Vector and Matrix

This last section of this post is going to cover calculating the sum of values in a vector and a matrix.

I can calculate the sum of the values in a vector or matrix using the sum() function.

Let’s recall the original vector I created called titanic.

A vector created called titanic.

I want to add these values in the vector. To do that, I would write this:

The sum of the values in the titanic vector.

As you can see the sum of this vector is 2422. What if I wanted to calculate all the values of the titanic row of my matrix?

Calculate all the values in the row called titanic in my matrix called entire_matrix.

Here the sum of value in my titanic row is 3081. Why are the two sums different? Remember that I added the domestic_gross_millions column to my matrix after the matrix was created. The original vector does not include the value for domestic_gross_millions.

Just as I did the sum of the values in a row, I can do the same for a column. If I want to add up all the values in domestic_opening_millions column, I would type the following:

Calculating the domestic opening amounts for all the films.

So the sum of all the values in the domestic_opening_millions column is 1177. This means that combined opening weekend total for all the films is about $1,117,000,000!

This just about does it for matrices in R! For the next post, I’ll get into lists in R.

One thought on “Working With Matrices

Comments are closed.