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.
- What is a “list” object?
- How do we reference elements from a list?
- What is the “mode” of a list?
- What’s the difference between a list and a data frame?
- Name two ways we can return the number of columns of a data frame.
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, ]
- What is the difference between the head and tail functions?
- A “list” is a simple R object that can contain any number of objects of any “class.”
- 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[] 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).
- 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.”
- 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.
- 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.
- 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).
- 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.