RPR-OBJECTS-Lists

From "A B C"
Revision as of 15:45, 24 September 2017 by Boris (talk | contribs)
Jump to navigation Jump to search

Abstract

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


 


This unit ...

Prerequisites

You need to complete the following units before beginning this one:


 


Objectives

This unit will ...

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


 


Outcomes

After working through this unit you ...

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


 


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.


 


Evaluation

Evaluation: NA

This unit is not evaluated for course marks.


 


Contents

Task:

  • 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.


 

Lists

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)
pUC19[[1]]
pUC19[[2]]
pUC19$ori
pUC19$BanI[2]

Note that in 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!


Task:

  1. Create a list like we did above for pUC19, but with the data for pACYC184.
  2. Confirm that it looks the same as the pUC19 one.
  3. Make a new list, call it plasmidDB that contains both the puc19 and pACYC184 lists.
  4. Add a third element to that list using the pBR322 data.

Then:

  • retrieve the entire pACYC184 list;
  • retrieve the Ori data for all plasmids (hint: this is a "list slice").



 


 


Further reading, links and resources

 


Notes


 


Self-evaluation

Question 1

Execute: x <- strsplit(plasmidData$Sites, ", ") and analyze the result.

  • (A) What is plasmidData$Sites?
  • (B) What is x?
  • (C) Why does strsplit() have a list as return value, not a vector or a data frame?

Answer ...

  • (A) A vector of strings (character elements).
  • (B) A list of vectors.
  • (C) Because strsplit() is vectorized and operates on all elements of the vector plasmidData$Sites. But the function doesn't know in advance how many elements the result is going to have for each element. So it needs to output vectors of different lengths. And the only way to combine them into a single value (note: all R functions return only a single value) is by collecting them in a list.


 


 



 




 

If in doubt, ask! If anything about this learning unit is not clear to you, do not proceed blindly but ask for clarification. Post your question on the course mailing list: others are likely to have similar problems. Or send an email to your instructor.



 

About ...
 
Author:

Boris Steipe <boris.steipe@utoronto.ca>

Created:

2017-08-05

Modified:

2017-09-10

Version:

1.0

Version history:

  • 1.0 Completed to first live version
  • 0.1 Material collected from previous tutorial

CreativeCommonsBy.png This copyrighted material is licensed under a Creative Commons Attribution 4.0 International License. Follow the link to learn more.