A little arithmetic with matrices

Similar to what you have learned with vectors, the standard operators like +, -, /, *, etc. work in an element-wise way on matrices in R.

For example, 2 * my_matrix multiplies each element of my_matrix by two.

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.

Instruction

load(url("http://s3.amazonaws.com/assets.datacamp.com/course/intro_to_r/all_wars_matrix.RData")) # all_wars_matrix is available in your workspace all_wars_matrix # Estimate the visitors visitors <- # Print the estimate to the console # all_wars_matrix is available in your workspace all_wars_matrix # Estimate the visitors visitors <- all_wars_matrix / 5 # Print the estimate to the console visitors msg = "Do not change the contents of `all_wars_matrix`; this matrix has already been created for you in the workspace." test_object("all_wars_matrix", undefined_msg = msg, incorrect_msg = msg) test_object("visitors", incorrect_msg = "It looks like `visitors` is not correct. Simply divide `all_wars_matrix` by 5 and store the resulting matrix as `visitors`.") test_output_contains("visitors", incorrect_msg = "Don't forget to also print out `visitors` so you can have a look.") success_msg("Great! What do these results tell you? A staggering 92 million people went to see A New Hope in US theaters! Continue to the next exercise.")

The number of visitors is equal to all_wars_matrix divided by 5.

Previous: 3-8 | Selection of matrix elements

Next: 3-10 | A little arithmetic with matrices (2)

Back to Main