Adding a row

Just like every action has a reaction, every cbind() has an rbind(). (We admit, we are pretty bad with metaphors.)

Your R workspace, where all variables you defined 'live' (check out what a workspace is), has already been initialized and contains two matrices:

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.

Instruction

Use rbind() to paste together star_wars_matrix and star_wars_matrix2, in this order. Assign the resulting matrix to all_wars_matrix.

# Construct matrix box_office_all <- c(461, 314.4, 290.5, 247.9, 309.3, 165.8) movie_names <- c("A New Hope","The Empire Strikes Back","Return of the Jedi") col_titles <- c("US","non-US") star_wars_matrix <- matrix(box_office_all, nrow = 3, byrow = TRUE, dimnames = list(movie_names, col_titles)) # Construct matrix2 box_office_all2 <- c(474.5, 552.5, 310.7, 338.7, 380.3, 468.5) movie_names2 <- c("The Phantom Menace", "Attack of the Clones", "Revenge of the Sith") star_wars_matrix2 <- matrix(box_office_all2, nrow=3, byrow = TRUE, dimnames = list(movie_names2, col_titles)) # remove all except all_wars_matrix rm(box_office_all) rm(movie_names) rm(col_titles) rm(box_office_all2) rm(movie_names2) # star_wars_matrix and star_wars_matrix2 are available in your workspace star_wars_matrix star_wars_matrix2 # Combine both Star Wars trilogies in one matrix all_wars_matrix <- # star_wars_matrix and star_wars_matrix2 are available in your workspace star_wars_matrix star_wars_matrix2 # Combine both Star Wars trilogies in one matrix all_wars_matrix <- rbind(star_wars_matrix, star_wars_matrix2) msg = "Do not override the variables that have been defined for you in the workspace (`star_wars_matrix` and `star_wars_matrix2`)." test_object("star_wars_matrix", eq_condition = "equal", undefined_msg = msg, incorrect_msg = msg) test_object("star_wars_matrix2", eq_condition = "equal", undefined_msg = msg, incorrect_msg = msg) test_object("all_wars_matrix", incorrect_msg = "Did you use the `rbind()` correctly to create `all_wars_matrix()`? `rbind()` should take two arguments: `star_wars_matrix` and `star_wars_matrix2`, in this order.") success_msg("Wonderful! Continue with the next exercise and see how you can combine the results of the `rbind()` function with the `colSums()` function!")

Bind the two matrices together like this:

rbind(matrix1, matrix2)

Assign the result to all_wars_matrix.

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

Next: 3-7 | The total box office revenue for the entire saga

Back to Main