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.
c(new_hope, empire_strikes, return_jedi)
to combine the three vectors into one vector. Call this vector box_office
.matrix()
function to do this. The first argument is the vector box_office
, containing all box office figures. Next, you'll have to specify nrow = 3
and byrow = TRUE
. Name the resulting matrix star_wars_matrix
.
# Box office Star Wars (in millions!)
new_hope <- c(460.998, 314.4)
empire_strikes <- c(290.475, 247.900)
return_jedi <- c(309.306, 165.8)
# Create box_office
box_office <-
# Construct star_wars_matrix
star_wars_matrix <-
# Box office Star Wars (in millions!)
new_hope <- c(460.998, 314.4)
empire_strikes <- c(290.475, 247.900)
return_jedi <- c(309.306, 165.8)
# Create box_office
box_office <- c(new_hope, empire_strikes, return_jedi)
# Construct star_wars_matrix
star_wars_matrix <- matrix(box_office, nrow = 3, byrow = TRUE)
msg <- "Do not change anything about the box office variables `new_hope`, `empire_strikes` and `return_jedi`!"
test_object("new_hope", undefined_msg = msg, incorrect_msg = msg)
test_object("empire_strikes", undefined_msg = msg, incorrect_msg = msg)
test_object("return_jedi", undefined_msg = msg, incorrect_msg = msg)
test_object("box_office", incorrect_msg = "Have you correctly combined the values of `new_hope`, `empire_strikes` and `return_jedi` into the vector `box_office`?")
test_function("matrix", c("data", "nrow", "byrow"),
incorrect_msg = "Make sure to correctly specify the arguments you pass to `matrix()`: `box_office`, `nrow = 3`, `by_row = TRUE`.")
test_object("star_wars_matrix",
incorrect_msg = "Did you assign the result of the `matrix()` call to `star_wars_matrix`?")
success_msg("The force is actually with you! Continue to the next exercise.")
box_office <- c(new_hope, empire_strikes, return_jedi)
will combine all numbers in the different vectors into a single vector with 6 elements.matrix(box_office, nrow = ..., by_row ...)
is a template for the solution to the second instruction.