39  List

Tsy voatery rectangle!

A list is the most flexible object for storing data. Following the building metaphor, a vector (Chapter 35) represents a tall building where every floor contains identical units, such as single-bedroom apartments, while a matrix (Chapter 36) represents a building with multiple identical units on each floor. In contrast, a list is like a multistory building where each floor can serve an entirely different purpose.

For instance, the first floor can be a swimming pool, the second floor a library, the third floor three separate apartments, and the fifth floor an entire secondary multistory building.

While such a building is unrealistic in architecture, it highlights the extreme flexibility of lists. Translating this back to programming, each list element must consist of data types introduced earlier. For instance, the first element of a list can be a character vector, the second a single numeric value, the third a matrix, and the fourth another list.

As a consequence, lists are not designed for direct mathematical calculations and can be non-trivial to manipulate because they lack a uniform rectangular structure.

Formal

A list is constructed with the function list().

# Construct a list where each argument defines an element name and its associated R object
my_list <- list(
  name1 = object1, # First element containing object1
  name2 = object2, # Second element containing object2
  name3 = object3  # Third element containing object3
)

Unlike matrices or data frames, there are no structural constraints on the types or dimensions of the stored objects; elements may consist of vectors of varying lengths, matrices, data frames, or other lists.

Objects can be accessed directly using double square brackets [[]], for instanmce my_list[[1]] or my_list[["name1"]], which extract the underlying object. If the elements are named, my_list$name1 can also be used as a convenient shortcut.

Single square brackets my_list[1] or my_list["name1"] subset the structure, returning a smaller list containing the specified element rather than the object itself.

Examples

# Construct a list containing numeric, list, and matrix objects
silly_example <- list(
  mine = 1,                              # First element: single numeric value
  list(yours = c(1, 2), hers = "A"),     # Second element: unnamed nested list
  mat = matrix(1:6, nrow = 2)            # Third element: 2x3 numeric matrix
)
  • Unlike a vector or a matrix, a list is displayed differently when printed.

    # Print the complete list object to observe its hierarchical output structure
    silly_example
    $mine
    [1] 1
    
    [[2]]
    [[2]]$yours
    [1] 1 2
    
    [[2]]$hers
    [1] "A"
    
    
    $mat
         [,1] [,2] [,3]
    [1,]    1    3    5
    [2,]    2    4    6
  • Extracting one element: double vs. single bracket

    # Extract the value of the first element
    silly_example[[1]]
    [1] 1
    str(silly_example[[1]])
     num 1
    # Subset the list to include only the first element
    silly_example[1]
    $mine
    [1] 1
    str(silly_example[1])
    List of 1
     $ mine: num 1

    Comparing the output reveals that double brackets ([[1]]) extract the underlying value directly, whereas single brackets ([1]) return a sublist containing the target element (indicated by $mine in the output). This structural difference can be verified using the str() function.

  • Extracting named elements and nested components

    # Extract the named element "xx"
    silly_example$mine
    [1] 1
    # Extract the 2nd element
    silly_example[[2]]
    $yours
    [1] 1 2
    
    $hers
    [1] "A"
    # Subset the 1st element within the 2nd element
    silly_example[[2]][1]
    $yours
    [1] 1 2
    # Extract component x from the nested list using its name
    silly_example[[2]]$yours
    [1] 1 2
    # Extract component x from the nested list using double brackets
    silly_example[[2]][[1]]
    [1] 1 2
    # Extract the 2nd column of the matrix stored as the 3rd element
    silly_example[[3]][, 2]
    [1] 3 4

Tips and warnings

Lists are rarely constructed manually unless specific requirements arise, such as building custom objects or tailored data structures. However, understanding list construction helpl accessing and manipulating the outputs of statistical analyses.