CH 3,4
CH 3,4
You can construct a matrix in R with the matrix() function. Consider the following
example:
The first argument is the collection of elements that R will arrange into the rows
and columns of the matrix. Here, we use 1:9 which is a shortcut for c(1, 2, 3,
4, 5, 6, 7, 8, 9).
The argument byrow indicates that the matrix is filled by the rows. If we want the
matrix to be filled by the columns, we just place byrow = FALSE.
The third argument nrow indicates that the matrix should have three rows.
Code:
matrix(1:9,byrow=TRUE,nrow=3)
Analyze matrices, you shall
It is now time to get your hands dirty. In the following exercises you will analyze the box
office numbers of the Star Wars franchise. May the force be with you!
In the editor, three vectors are defined. Each one represents the box office numbers
from the first three Star Wars movies. The first element of each vector indicates the US
box office revenue, the second element refers to the Non-US box office (source:
Wikipedia).
In this exercise, you'll combine all these figures into a single vector. Next, you'll build a
matrix from this vector.
Instructions
100 XP
Code:
# Create box_office
# Construct star_wars_matrix
Similar to vectors, you can add names for the rows and the columns of a matrix
We went ahead and prepared two vectors for you: region, and titles. You will need
these vectors to name the columns and rows of star_wars_matrix, respectively.
Code:
# Construct matrix
titles <- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi")
star_wars_matrix
Calculating the worldwide box office
The single most important thing for a movie in order to become an instant legend in
Tinseltown is its worldwide box office figures.
To calculate the total box office revenue for the three Star Wars movies, you have to
take the sum of the US revenue column and the non-US revenue column.
In R, the function rowSums() conveniently calculates the totals for each row of a matrix.
This function creates a new vector:
rowSums(my_matrix)
Code:
# Construct star_wars_matrix
dimnames = list(c("A New Hope", "The Empire Strikes Back", "Return of the Jedi"),
c("US", "non-US")))
You can add a column or multiple columns to a matrix with the cbind() function, which
merges matrices and/or vectors together by column. For example:
Add worldwide_vector as a new column to the star_wars_matrix and assign the result
to all_wars_matrix. Use the cbind() function.
Code:
# Construct star_wars_matrix
dimnames = list(c("A New Hope", "The Empire Strikes Back", "Return of the Jedi"),
c("US", "non-US")))
Your R workspace, where all variables you defined 'live' (check out what a workspace
is), has already been initialized and contains two matrices:
star_wars_matrix that we have used all along, with data on the original trilogy,
star_wars_matrix2, with similar data for the prequels trilogy.
Type the name of these matrices in the console and hit Enter if you want to have a
closer look. If you want to check out the contents of the workspace, you can type ls() in
the console.
Instructions
100 XP
Code:
star_wars_matrix
star_wars_matrix2
Code:
all_wars_matrix
total_revenue_vector
Selection of matrix elements
Similar to vectors, you can use the square brackets [ ] to select one or multiple
elements from a matrix. Whereas vectors have one dimension, matrices have two
dimensions. You should therefore use a comma to separate the rows you want to select
from the columns. For example:
my_matrix[1,2] selects the element at the first row and second column.
my_matrix[1:3,2:4] results in a matrix with the data on the rows 1, 2, 3 and
columns 2, 3, 4.
If you want to select all elements of a row or a column, no number is needed before or
after the comma, respectively:
Back to Star Wars with this newly acquired knowledge! As in the previous
exercise, all_wars_matrix is already available in your workspace.
Select the non-US revenue for all movies (the entire second column
of all_wars_matrix), store the result as non_us_all.
Use mean() on non_us_all to calculate the average non-US revenue for all
movies. Simply print out the result.
This time, select the non-US revenue for the first two movies in all_wars_matrix.
Store the result as non_us_some.
Use mean() again to print out the average of the values in non_us_some.
Code:
all_wars_matrix
mean(non_us_all)
mean(non_us_some)
As a newly-hired data analyst for Lucasfilm, it is your job to find out how many visitors
went to each movie for each geographical area. You already have the total revenue
figures in all_wars_matrix. Assume that the price of a ticket was 5 dollars. Simply
dividing the box office numbers by this ticket price gives you the number of visitors.
Code:
all_wars_matrix
visitors
A little arithmetic with matrices (2)
Just like 2 * my_matrix multiplied every element of my_matrix by two, my_matrix1 *
my_matrix2 creates a matrix where each element is the product of the corresponding
elements in my_matrix1 and my_matrix2.
After looking at the result of the previous exercise, big boss Lucas points out that the
ticket prices went up over time. He asks to redo the analysis based on the prices you
can find in ticket_prices_matrix (source: imagination).
Those who are familiar with matrices should note that this is not the standard matrix
multiplication for which you should use %*% in R.
Code:
all_wars_matrix
ticket_prices_matrix
# US visitors
mean(us_visitors)