Knowledge Quiz: Lists

Consider the following list, l1.

$name
[1] "bob"

$age
[1] 25

$height
[1] 180

$eyes
[1] "blue"

Which of the following expressions will change the value of the eyes slot in l1 from “blue” to “green”?

  • Wrong: This command doesn’t actually change anything.
  • Correct
  • Wrong: There is no slot named “4” in l1.
  • Wrong: This command tries to replace the value of l4$eyes with the contents of the object green, not the character vector "green".
  • Correct

There is a problem with the list shown below (l): the information for eye color is wrongly saved in a slot called hair. Which of the following options will change the name of the hair slot to something more appropriate?

$name
[1] "bob"

$age
[1] 25

$height
[1] 180

$hair
[1] "blue"

The correct way to change the name of the value is names(l)[4] <- "eyes"

  • Wrong: names("eyes") is NULL since a character string doesn’t have a name, so this command will just erase the list element
  • Wrong: This command will return an error since the names() element is a vector and not a list
  • Correct
  • Wrong: This command will change the value itself and not the name
  • Wrong: This command will change the value itself and not the name

Given the list l <- list(a = 1, b = 2, c = 3), which statement correctly describes the difference between l["a"] and l[["a"]]?

Single brackets ([]) always preserve the list “wrapper” around the result, even when selecting just one slot. So l["a"] returns a one-element list. Double brackets ([[]]) reach inside and return the actual contents of a single slot. So l[["a"]] returns the numeric vector 1.

Note
  • Click the check-mark button to check your answer.
  • Click the question-mark button to see an explanation of the solution.
Back to top