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)

Instruction

# Construct star_wars_matrix box_office <- c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8) star_wars_matrix <- matrix(box_office, nrow = 3, byrow = TRUE, dimnames = list(c("A New Hope", "The Empire Strikes Back", "Return of the Jedi"), c("US", "non-US"))) # Calculate worldwide box office figures worldwide_vector <- # Construct star_wars_matrix box_office <- c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8) star_wars_matrix <- matrix(box_office, nrow = 3, byrow = TRUE, dimnames = list(c("A New Hope", "The Empire Strikes Back", "Return of the Jedi"), c("US", "non-US"))) # Calculate worldwide box office figures worldwide_vector <- rowSums(star_wars_matrix) msg <- "Do not change anything about the preset variables `box_office_all` and `star_wars_marix`!" test_object("box_office", undefined_msg = msg, incorrect_msg = msg) test_object("star_wars_matrix", undefined_msg = msg, incorrect_msg = msg) test_object("worldwide_vector", incorrect_msg = "Call `rowSums()` on `star_wars_matrix` and store the result in `worldwide_vector`.") success_msg("Well done! Continue to the next exercise.")

rowSums(star_wars_matrix) will calculate the sum of every row, so the total box office for each movie.

Previous: 3-3 | Naming a matrix

Next: 3-5 | Adding a column for the Worldwide box office

Back to Main