Home > Articles

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

This chapter is from the book

Q&A

  • Q. Can we create nested lists?

    A. Yes. Because lists can store any type of object, they can themselves store other lists. Here’s an example:

    > nestedList <- list(A = 1, B = list(C = 3, D = 4))  # Create a nested list
    > nestedList                                         # Print the nested list
    $A
    [1] 1
    
    $B
    $B$C
    [1] 3
    
    $B$D
    [1] 4
    
    
    > nestedList$B$C                 # Extract the C element within the B element
    [1] 3
  • Q. What other inputs can we use within the double square brackets?

    A. In the last hour, you saw that you can use integers to directly reference elements of a list. Refer to the help file (opened using ?"[[") for a complete list of possible inputs. However, it is worth nothing that you can use single-character strings to reference columns. Here’s an example:

    > weather           # The full dataset
            Day  Date TempF TempC
    1  Saturday Jul 4    75    24
    2    Sunday Jul 5    86    30
    3    Monday Jul 6    83    28
    4   Tuesday Jul 7    83    28
    5 Wednesday Jul 8    87    31
    > col <- "TempC"    # The column we want to select
    > weather[[col]]    # Return the TempC column
    [1] 24 30 28 28 31
  • Q. What is the difference between DF[ ] and DF[ , ]?

    A. As shown previously, you subscript data from a data frame using square brackets. Here’s an example:

    > weather [ , c("Day", "TempC") ]   # All rows, 2 columns
            Day TempC
    1  Saturday    24
    2    Sunday    30
    3    Monday    28
    4   Tuesday    28
    5 Wednesday    31

    In this example, we provide two subscripts for the data frame: blank for the rows (so all rows are returned) and a character vector to select two columns. The subscripts are separated by a comma. If we omit the comma, we appear to get the same result:

    > weather [ c("Day", "TempC") ]     # 2 vector elements
            Day TempC
    1  Saturday    24
    2    Sunday    30
    3    Monday    28
    4   Tuesday    28
    5 Wednesday    31

    Here, we are using the fact that a data frame is actually a named list of vectors. In this case, we are creating a “sub-list” containing only the two columns specified.

  • Q. Why, when I select a single column, is it returned as a vector?

    A. When you select a single column via the square brackets approach, it is indeed returned as a vector:

    > weather [ , c("Day", "TempC") ]   # 2 columns - returns a data frame
            Day TempC
    1  Saturday    24
    2    Sunday    30
    3    Monday    28
    4   Tuesday    28
    5 Wednesday    31
    > weather [ , "TempC" ]             # 1 column - returns a vector
    [1] 24 30 28 28 31

    In this case, the last line is equivalent to weather$TempC. When you select a single column of data, R simplifies the output in a way that’s similar to how you saw matrix dimensions dropped in Hour 3. If you specifically want to retain the dimensional structure, you can use the argument drop within the square brackets, as follows:

    > weather [ , "TempC", drop = F ]   # 1 column - retain dimensions
      TempC
    1    24
    2    30
    3    28
    4    28
    5    31

    As you can see from the output, the use of drop = F retains the structure, returning a 5×1 data frame.

  • + Share This
  • 🔖 Save To Your Account