# your commandsPS2: Matrices and Lists
STAT 133
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:
"e"
- Matrix 2x2 with
"e", "h"as a first row and"f", "i"as a second row
# your commands
- Vector
"a" "g" "m".
# your commands
- Matrix 3x3 in which the original 2nd and 4th columns are excluded.
# your commands
- 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 commands2 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 code3 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
- What is the difference between
list_example["letter"]andlist_example[["letter"]]? Explain.
Your Answer:
- What does
list_example$letter[3]return? Why?
Your Answer:
- What does the command
list_example$value = NULLdo? Explain.
Your Answer:
- 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.
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 code5 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 code5.2 Subscripting (aka Indexing, Slicing) a Matrix
Refer to the matrix HEROES.
- Using
HEROES, write a command that gives you the first 3 rows, and columnsheightandweight.
# your code
- 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
- Using
HEROES, write a command that gives you the name of the youngest superhero. Hint:which.min()is your friend.
# your code
- 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 functionsum()is your friend.
# your code
- Using
HEROES, write a command that arranges its content byweightin decreasing order. Hint: the functionorder()is your friend.
# your code
- Add a new row to
HEROESwith the data of Aquaman given below. Hint: therbind()function is your friend. Display the updatedHEROESmatrix. To clarify:HEROESshould 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
- Add a column
ratiotoHEROES. The values ofratioshould be the ratio of weight over height. Hint: thecbind()function is your friend. Display the updatedHEROESmatrix. To clarify:HEROESshould 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 code6.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.
- What is the name of the superhero with smallest strength level? Hint: the function
which.min()is your friend.
# your code
- How many superheroes have an unknown
agevalue? Hint: the functionis.na()is your friend.
# your code
- What is the median age of superheroes (ignore missing values)?
# your code
- What is the height of the superhero with the smallest weight value? Hint: the function
which.min()is your friend.
# your code
- How many superheroes have height values between 1.9 and 2.5 meters (inclusive)? Hint: the function
sum()is your friend.
# your code
- 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