Naming a matrix

To help you remember what is stored in star_wars_matrix, you would like to add the names of the movies for the rows. Not only does this help you to read the data, but it is also useful to select certain elements from the matrix.

Similar to vectors, you can add names for the rows and the columns of a matrix

rownames(my_matrix) <- row_names_vector
colnames(my_matrix) <- col_names_vector

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.

Instruction

# 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) # Construct matrix star_wars_matrix <- matrix(c(new_hope, empire_strikes, return_jedi), nrow = 3, byrow = TRUE) # Vectors region and titles, used for naming region <- c("US", "non-US") titles <- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi") # Name the columns with region # Name the rows with titles # Print out 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) # Construct matrix star_wars_matrix <- matrix(c(new_hope, empire_strikes, return_jedi), nrow = 3, byrow = TRUE) # Vectors region and titles, used for naming region <- c("US", "non-US") titles <- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi") # Name the columns with region colnames(star_wars_matrix) <- region # Name the rows with titles rownames(star_wars_matrix) <- titles # Print out star_wars_matrix star_wars_matrix 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) msg <- "Don't change the contents of `star_wars_matrix`; only the names of the rows and columns!" test_object("star_wars_matrix", incorrect_msg = msg) msg <- "Don't change anything about the `region` and `titles` vectors that have been defined for you." test_object("region", undefined_msg = msg, incorrect_msg = msg) test_object("titles", undefined_msg = msg, incorrect_msg = msg) test_object("star_wars_matrix", eq_condition = "equal", incorrect_msg = "Did you set the row and column names of `star_wars_matrix` correctly? Use `colnames(star_wars_matrix) <- region` for the column names; do a similar thing to name the rows.") test_output_contains("star_wars_matrix", incorrect_msg = "Don't forget to print out `star_wars_matrix` after you've named the rows and columns.") success_msg("Great! You're on the way of becoming an R jedi! Continue to the next exercise.")

You can use colnames(star_wars_matrix) <- region to name the columns of star_wars_matrix. Do a similar thing to name the rows.

Previous: 3-2 | Analyze matrices, you shall

Next: 3-4 | Calculating the worldwide box office

Back to Main