Chapter 9 Lists

(R Lists)

9.1 Overview

9.1.1 Abstract:

Introduction to R list data types: properties, how to create, and modify them and how to retrieve data.

9.1.2 Objectives:

This unit will:

  • introduce R lists;
  • cover a number of basic operations.

9.1.3 Outcomes:

After working through this unit you:

  • know how to create and manipulate lists;
  • can extract items from lists.

9.1.4 Deliverables:

Time management: Before you begin, estimate how long it will take you to complete this unit. Then, record in your course journal: the number of hours you estimated, the number of hours you worked on the unit, and the amount of time that passed between start and completion of this unit.

Journal: Document your progress in your Course Journal. Some tasks may ask you to include specific items in your journal. Don't overlook these.

Insights: If you find something particularly noteworthy about this unit, make a note in your insights! page.

9.2 Lists

9.3 Task 18

Load the R-Exercise_BasicSetup project in RStudio if you don't already have it open. Type init() as instructed after the project has loaded. Continue below.

The elements of matrices and arrays all have to be of the same type. Data frames allow us to store elements of different types in columns, but all columns have to have the same length and the elements have to be "atomic" - i.e. you can't put vectors into dataframe columns. But R's lists are much more versatile. They are simply ordered collections of components. These components can have different type - all kinds of R objects can go into a list: characters, booleans, any kind of numeric data, even functions - AND they can have different size.

Lists are created with the list() function, which works similar to the c() function for vectors. Components are accessed through their index in double square brackets, or through their name, using the "$" operator, if the name has been defined. Here is an example:

pUC19 <- list(size=2686, marker="ampicillin", ori="ColE1", accession="L01397", BanI=c(235, 408, 550, 1647) )
objectInfo(pUC19)
## object contents:$size
## [1] 2686
## 
## $marker
## [1] "ampicillin"
## 
## $ori
## [1] "ColE1"
## 
## $accession
## [1] "L01397"
## 
## $BanI
## [1]  235  408  550 1647
## 
## 
## structure of object:
## List of 5
##  $ size     : num 2686
##  $ marker   : chr "ampicillin"
##  $ ori      : chr "ColE1"
##  $ accession: chr "L01397"
##  $ BanI     : num [1:4] 235 408 550 1647
## 
## attributes:
## $names
## [1] "size"      "marker"    "ori"       "accession" "BanI"
pUC19[[1]]
## [1] 2686
pUC19[[2]]
## [1] "ampicillin"
pUC19$ori
## [1] "ColE1"
pUC19$BanI[2]
## [1] 408

Note that if we stored multiple restriction enzymes in one string, separated by commas, in our data frame example. While we can take such strings apart again, by using the strsplit() function, the string itself still has to be one single element in the data frame's column. Lists have no such restriction. In our example above, we assigned a vector of restriction site positions to the element "BanI".

You can easily imagine that we could now create a list of lists, and that list of lists could hold an entire plasmid database in a most versatile way. Let's do this!

9.4 Task 19

  • Create a list like with data for pACYC184 following the structure for the pUC19 example but using only size, marker and ori data:

    • size: 4245
    • marker: Tet, Cam
    • ori: p15A
  • Confirm that your new list's structure looks the pUC19 one (minus "accession", and the "BanI"" element).

  • Make a new list, call it plasmidDB and assign to it the puc19 list:
    • plasmidDB <- list()
    • plasmidDB[["pUC19"]] <- pUC19
  • Add your pACYC184 list
  • Add a third element to plasmidDB, "pBR322" using the pBR322 data:
    • size: 4361
    • marker: Amp, Tet
    • ori: ColE1
  • Then: retrieve the entire pACYC184 list
  • Retrieve all sizes from the list. Use unlist() to flatten the result. Then use min() to find the size of the smallest one. 1


  1. Lost? Solutions here (but don't peek).

Whereas data frames allow you to get all data from a column directly, this is not possible for lists. You need a function that iterates over all list elements instead. Such a function is lapply(), one of several "apply" functions. For example, to get all "ori" elements, try:

plasmidDB <- list()
plasmidDB[["pUC19"]] <- pUC19
lapply(plasmidDB, function(x) { return(x$ori) })
## $pUC19
## [1] "ColE1"

9.5 Self-evaluation

  • Question 1: Execute: x <- strsplit(plasmidData$Sites, ", ") and analyze the result.
  1. What is plasmidData$Sites?
  2. What is x?
  3. Why does strsplit() have a list as return value, not a vector or a data frame?

  1. and when you click on the arrow to the left, this will take you back to where you came from