Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Workshop

The workshop contains quiz questions and exercises to help you solidify your understanding of the material covered. Try to answer all questions before looking at the “Answers” section that follows.

Quiz

  1. What is a “list” object?
  2. How do we reference elements from a list?
  3. What is the “mode” of a list?
  4. What’s the difference between a list and a data frame?
  5. Name two ways we can return the number of columns of a data frame.
  6. If we run the following code, what would the contents and structure of result1 and result2 contain?

    > myDf <- data.frame(X = -2:2, Y = 1:5)
    > result1 <- myDf$Y [ myDf$X > 0 ]
    > result2 <- myDf [ myDf$X > 0, ]
  7. What is the difference between the head and tail functions?

Answers

  1. A “list” is a simple R object that can contain any number of objects of any “class.”
  2. We can reference elements of a list using the “double square brackets” notation. Most commonly, we provide the index of the element we want to return from the list (for example, myList[[2]] for the second element). If a list has element names, we can alternatively use the dollar notation, specifying the name of the list element (for example, myList$X to return the X element of myList).
  3. Because a list is a “multi-mode” object, it has no explicit “mode.” If you ask for a list’s mode, it simply returns “list.”
  4. A list can contain any number of objects of any class—its elements may be named or unnamed. A data frame is a “named” list that is restricted to contain only same-length vectors—when printing a data frame, it uses a specific method so the data is presented in a more formatted manner.
  5. We can use the length function to return the number of columns in a data frame, because this returns the number of vector elements in the underlying “list” structure. Alternatively, because we can treat a data frame as a matrix, we can use the ncol function to achieve the same result.
  6. The result1 object will contain a vector of those values from the Y column where the corresponding X column is greater than 0—specifically, this will be a vector containing values 4 and 5. The result2 object will contain a data frame with two rows, corresponding to the rows where X is greater than 0 (so rows 4 and 5 of the original data frame).
  7. The head function returns the first six rows (by default) of a data frame. The tail function returns the last six rows (by default) of a data frame.
  • + Share This
  • 🔖 Save To Your Account