PS2: Matrices and Lists

STAT 133

General Instructions
  • Use the template file ps2.qmd to answer the questions.

  • Rename this file as ps2-first-last.qmd, where first and last are your first and last names (e.g. ps2-gaston-sanchez.qmd).

  • Submit both your qmd and HTML files to the corresponding assignment submission in bCourses.

1 Subsetting Matrices

Consider the matrix mat = matrix(letters[1:15], nrow = 3, ncol = 5) for the following questions.

For each of the following parts, write a subsetting command that produce the given outputs:

  1. "e"
# your commands


  1. Matrix 2x2 with "e", "h" as a first row and "f", "i" as a second row
# your commands


  1. Vector "a" "g" "m".
# your commands


  1. Matrix 3x3 in which the original 2nd and 4th columns are excluded.
# your commands


  1. Matrix 3x5 in which the order of the rows is reversed (i.e. row 1 becomes row 3, row 2 becomes row 2, row 3 becomes row 1).
# your commands

2 Creating a List

Consider an R list cass that when printed it gets displayed as follows:

$name
    first    middle      last 
"Cassian"   "Jeron"   "Andor" 

$skills
[1] "captain" "pilot"   "lier"    "shooter"

$cargo
    X  Y
a 101 90
b 201 80
c 301 70

$specs
$specs$species
[1] "human"

$specs$gender
[1] "male"

$specs$height
[1] 1.78

Write code to create the list cass, and display this object (i.e. print it). Notice that name is a named character vector, skills is a character vector with unnamed elements, cargo is a matrix with row and column names, and specs is a list with 3 named elements.

# your code

3 Subsetting Lists

Consider the following list:

list_example = list("letter" = c("A", "B", "C"), "value" = c(10, 50, 20))
list_example
$letter
[1] "A" "B" "C"

$value
[1] 10 50 20
  1. What is the difference between list_example["letter"] and list_example[["letter"]]? Explain.

Your Answer:


  1. What does list_example$letter[3] return? Why?

Your Answer:


  1. What does the command list_example$value = NULL do? Explain.

Your Answer:


  1. What does the command list_example$lgl = c(TRUE, FALSE, TRUE) do? Explain.

Your Answer:



4 Superheroes

Consider the following image containing data of a handful of superheroes.

Figure 1: Superheros Data

4.1 Creating Vectors

Create vectors for the columns in the data table displayed above, according to the following data types. If there are missing values, encode them as NA.

  • name: character vector (name of superhero)

  • age: integer vector (years)

  • height: real (i.e. double) vector (meters)

  • weight: real (i.e. double) vector (kg)

  • strength: integer vector

# your code

5 Matrix of Superheros

5.1 Creating a Matrix

Use the vectors created in the preceding part to create a matrix HEROES. This matrix should have dimensions: 5 rows, and 4 columns (age, height, weight, and strength). Also, give names to both the rows and columns of HEROES according to figure 1; and display this matrix.

# your code

5.2 Subscripting (aka Indexing, Slicing) a Matrix

Refer to the matrix HEROES.

  1. Using HEROES, write a command that gives you the first 3 rows, and columns height and weight.
# your code


  1. Using HEROES, write a command that gives you the following output (see below): age values arranged in decreasing order. Hint: sort() is your friend.
  Batman     Hulk  Ironman Superman 
      40       35       28       18
# your code


  1. Using HEROES, write a command that gives you the name of the youngest superhero. Hint: which.min() is your friend.
# your code


  1. Using HEROES, write a command that gives you the number of superheroes that have a strength level less than or equal to 2. Hint: the function sum() is your friend.
# your code


  1. Using HEROES, write a command that arranges its content by weight in decreasing order. Hint: the function order() is your friend.
# your code


  1. Add a new row to HEROES with the data of Aquaman given below. Hint: the rbind() function is your friend. Display the updated HEROES matrix. To clarify: HEROES should have 6 rows (Aquaman’s data in row 6), and it should also include Aquaman’s name in the row names.
  • name: Aquaman
  • age: 35
  • height: 1.85
  • weight: 147
  • strength: 3
# your code


  1. Add a column ratio to HEROES. The values of ratio should be the ratio of weight over height. Hint: the cbind() function is your friend. Display the updated HEROES matrix. To clarify: HEROES should have 6 rows and 5 columns.
# your code



6 List of Superheroes

Create a list heroes containing the vectors created in problem 4. This list should have five elements (one vector per element). Make sure each element of the list has the corresponding name so that you can refer to them by invoking either heroes$name or heroes[["name"]]. Display your heroes list.

# your code

6.1 Subscripting (aka Indexing, Slicing) a List

Refer to the list heroes. Write R commands—displaying the output—that answer the following questions. Answer each part in its individual code chunk.

  1. What is the name of the superhero with smallest strength level? Hint: the function which.min() is your friend.
# your code


  1. How many superheroes have an unknown age value? Hint: the function is.na() is your friend.
# your code


  1. What is the median age of superheroes (ignore missing values)?
# your code


  1. What is the height of the superhero with the smallest weight value? Hint: the function which.min() is your friend.
# your code


  1. How many superheroes have height values between 1.9 and 2.5 meters (inclusive)? Hint: the function sum() is your friend.
# your code


  1. Sort the superheroes’ names arranged—from smallest to largest—by their weight/height (“weight over height”) values. Hint: the function order() is your friend.
# your code