5.4 Managing Lists

A list is an R structure that allows you to combine elements of different types, including lists embedded in a list, and length. Many statistical outputs are provided as a list as well; therefore, its critical to understand how to work with lists. In this section I will guide you throught the basics of managing lists to include:

5.4.2 Adding on to Lists

To add additional list components to a list we can leverage the list() and append() functions. We can illustrate with the following list.

If we add the new elements with list() it will create a list of two components, component 1 will be a nested list of the original list and component 2 will be the new elements added:

To simply add a 4th list component without creating nested lists we use the append() function:

Alternatively, we can also add a new list component by utilizing the ‘$’ sign and naming the new item:

To add individual elements to a specific list component we need to introduce some subsetting, we’ll continue with our original l1 list:

To add additional values to a list item you need to subset for that specific list item and then you can use the c() function to add the additional elements to that list item:

5.4.3 Adding Attributes to Lists

The attributes that you can add to lists include names, general comments, and specific list item comments. Currently, our l1 list has no attributes:

We can add names to lists in two ways. First, we can use names() to assign names to list items in a pre-existing list. Second, we can add names to a list when we are creating a list.

We can also add comments to lists. As previously mentioned, comments act as a note to the user without changing how the object behaves. With lists, we can add a general comment to the list using comment() and we can also add comments to specific list items with attr().

5.4.4 Subsetting Lists

If list x is a train carrying objects, then x[[5]] is the object in car 5; x[4:6] is a train of cars 4-6 Twitter - ‘(???)

To subset lists we can utilize the single bracket [ ], double brackets [[ ]], and dollar sign $ operators. Each approach provides a specific purpose and can be combined in different ways to achieve the following subsetting objectives:

  • Subset list and preserve output as a list
  • Subset list and simplify output
  • Subset list to get elements out of a list
  • Subset list with a nested list

5.4.4.2 Subset list and simplify output

To extract one or more list items while simplifying13 the output use the [[ ]] or $ operator:

One thing that differentiates the [[ operator from the $ is that the [[ operator can be used with computed indices. The $ operator can only be used with literal names.

5.4.4.3 Subset list to get elements out of a list

To extract individual elements out of a specific list item combine the [[ (or $) operator with the [ operator:

5.4.5 Applying functions to lists

5.4.5.1 The lapply() function

The lapply() function does the following simple series of operations:

  1. it loops over a list, iterating over each element in that list

  2. it applies a function to each element of the list (a function that you specify)

  3. and returns a list (the l is for “list”).

The syntax for lapply() is as follows where

  • X is the list
  • FUN is the function to be applied
  • ... is for any other arguments to be passed to the function

To provide examples we’ll generate a list of four items:

The above provides a simple example where each list item is simply a vector of numeric values. However, consider the case where you have a list that contains data frames and you would like to loop through each list item and perform a function to the data frame. In this case we can embed an apply function within an lapply function.

For example, the following creates a list for R’s built in beaver data sets. The lapply function loops through each of the two list items and uses apply to calculate the mean of the columns in both list items. Note that I wrap the apply function with round to provide an easier to read output.

5.4.5.2 The sapply() function

The sapply() function behaves similarly to lapply(); the only real difference is in the return value. sapply() will try to simplify the result of lapply() if possible. Essentially, sapply() calls lapply() on its input and then applies the following algorithm:

  • If the result is a list where every element is length 1, then a vector is returned
  • If the result is a list where every element is a vector of the same length (> 1), a matrix is returned.
  • If neither of the above simplifications can be performed then a list is returned

To illustrate the differences we can use the previous example using a list with the beaver data and compare the sapply and lapply outputs:

beaver1 beaver2
day 346.20 307.13
time 1312.02 1446.20
temp 36.86 37.60
activ 0.05 0.62


  1. Its important to understand the difference between simplifying and preserving subsetting. Simplifying subsets returns the simplest possible data structure that can represent the output. Preserving subsets keeps the structure of the output the same as the input. See Hadley Wickham’s section on Simplifying vs. Preserving Subsetting to learn more.

  2. Its important to understand the difference between simplifying and preserving subsetting. Simplifying subsets returns the simplest possible data structure that can represent the output. Preserving subsets keeps the structure of the output the same as the input. See Hadley Wickham’s section on Simplifying vs. Preserving Subsetting to learn more.