Difference between revisions of "RPR-OBJECTS-Lists"

From "A B C"
Jump to navigation Jump to search
m
m (Boris moved page RPR-Objects-Lists to RPR-OBJECTS-Lists)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
<div id="BIO">
+
<div id="ABC">
  <div class="b1">
+
<div style="padding:5px; border:1px solid #000000; background-color:#b3dbce; font-size:300%; font-weight:400; color: #000000; width:100%;">
 
R "Lists"
 
R "Lists"
  </div>
+
<div style="padding:5px; margin-top:20px; margin-bottom:10px; background-color:#b3dbce; font-size:30%; font-weight:200; color: #000000; ">
 
+
(R Lists)
  {{Vspace}}
+
</div>
 
 
<div class="keywords">
 
<b>Keywords:</b>&nbsp;
 
R Lists
 
 
</div>
 
</div>
  
{{Vspace}}
+
{{Smallvspace}}
 
 
 
 
__TOC__
 
 
 
{{Vspace}}
 
 
 
 
 
{{LIVE}}
 
 
 
{{Vspace}}
 
  
  
</div>
+
<div style="padding:5px; border:1px solid #000000; background-color:#b3dbce33; font-size:85%;">
<div id="ABC-unit-framework">
+
<div style="font-size:118%;">
== Abstract ==
+
<b>Abstract:</b><br />
 
<section begin=abstract />
 
<section begin=abstract />
<!-- included from "./components/RPR-Objects-Lists.components.txt", section: "abstract" -->
 
 
Introduction to R list data types: properties, how to create, and modify them and how to retrieve data.
 
Introduction to R list data types: properties, how to create, and modify them and how to retrieve data.
 
<section end=abstract />
 
<section end=abstract />
 
+
</div>
{{Vspace}}
+
<!-- ============================ -->
 
+
<hr>
 
+
<table>
== This unit ... ==
+
<tr>
=== Prerequisites ===
+
<td style="padding:10px;">
<!-- included from "./components/RPR-Objects-Lists.components.txt", section: "prerequisites" -->
+
<b>Objectives:</b><br />
<!-- included from "./data/ABC-unit_components.txt", section: "notes-prerequisites" -->
 
You need to complete the following units before beginning this one:
 
*[[RPR-Objects-Data_frames|RPR-Objects-Data_frames (R "data frames"")]]
 
 
 
{{Vspace}}
 
 
 
 
 
=== Objectives ===
 
<!-- included from "./components/RPR-Objects-Lists.components.txt", section: "objectives" -->
 
 
This unit will ...
 
This unit will ...
 
* ... introduce R lists;
 
* ... introduce R lists;
 
* ... cover a number of basic operations.
 
* ... cover a number of basic operations.
 +
</td>
 +
<td style="padding:10px;">
 +
<b>Outcomes:</b><br />
 +
After working through this unit you ...
 +
* ... know how to create and manipulate lists;
 +
* ... can extract items from lists.
 +
</td>
 +
</tr>
 +
</table>
 +
<!-- ============================  -->
 +
<hr>
 +
<b>Deliverables:</b><br />
 +
<section begin=deliverables />
 +
<ul>
 +
<li><b>Time management</b>: 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.</li>
 +
<li><b>Journal</b>: Document your progress in your [[FND-Journal|Course Journal]]. Some tasks may ask you to include specific items in your journal. Don't overlook these.</li>
 +
<li><b>Insights</b>: If you find something particularly noteworthy about this unit, make a note in your [[ABC-Insights|'''insights!''' page]].</li>
 +
</ul>
 +
<section end=deliverables />
 +
<!-- ============================  -->
 +
<hr>
 +
<section begin=prerequisites />
 +
<b>Prerequisites:</b><br />
 +
<ul>
 +
This unit builds on material covered in the following prerequisite units:<br />
 +
*[[RPR-Objects-Data_frames|RPR-Objects-Data_frames (R "data frames"")]]
 +
</ul>
 +
<section end=prerequisites />
 +
<!-- ============================  -->
 +
</div>
  
{{Vspace}}
+
{{Smallvspace}}
  
  
=== Outcomes ===
 
<!-- included from "./components/RPR-Objects-Lists.components.txt", section: "outcomes" -->
 
After working through this unit you ...
 
* ... know how to create and manipulate lists;
 
* ... can extract items from lists.
 
  
{{Vspace}}
+
{{Smallvspace}}
  
  
=== Deliverables ===
+
__TOC__
<!-- included from "./components/RPR-Objects-Lists.components.txt", section: "deliverables" -->
 
<!-- included from "./data/ABC-unit_components.txt", section: "deliverables-time_management" -->
 
*<b>Time management</b>: 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.
 
<!-- included from "./data/ABC-unit_components.txt", section: "deliverables-journal" -->
 
*<b>Journal</b>: Document your progress in your [[FND-Journal|Course Journal]]. Some tasks may ask you to include specific items in your journal. Don't overlook these.
 
<!-- included from "./data/ABC-unit_components.txt", section: "deliverables-insights" -->
 
*<b>Insights</b>: If you find something particularly noteworthy about this unit, make a note in your [[ABC-Insights|'''insights!''' page]].
 
  
 
{{Vspace}}
 
{{Vspace}}
  
  
</div>
+
=== Evaluation ===
<div id="BIO">
+
<b>Evaluation: NA</b><br />
 +
<div style="margin-left: 2rem;">This unit is not evaluated for course marks.</div>
 
== Contents ==
 
== Contents ==
<!-- included from "./components/RPR-Objects-Lists.components.txt", section: "contents" -->
 
  
 
{{task| 1=
 
{{task| 1=
Line 94: Line 88:
 
Lists are created with the <code>list()</code> function, which works similar to the <code>c()</code> 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:
 
Lists are created with the <code>list()</code> function, which works similar to the <code>c()</code> 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:
  
<source lang="rsplus">
+
<pre>
  
 
pUC19 <- list(size=2686, marker="ampicillin", ori="ColE1", accession="L01397", BanI=c(235, 408, 550, 1647) )
 
pUC19 <- list(size=2686, marker="ampicillin", ori="ColE1", accession="L01397", BanI=c(235, 408, 550, 1647) )
Line 104: Line 98:
 
pUC19$BanI[2]
 
pUC19$BanI[2]
  
</source>
+
</pre>
  
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 <code>strsplit()</code> 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".
+
Note that in our <code>data.frame()</code> example, we stored multiple restriction enzymes in '''one''' string, separated by commas. While we can take such strings apart again, by using the <code>strsplit()</code> 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!
 
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!
Line 113: Line 107:
 
{{task|1=
 
{{task|1=
  
* Create a list like with data for pACYC184 following the structure for the pUC19 example but using only size, marker and ori data:
+
* Create a list like the one above with data for pACYC184 following the structure for the pUC19 example but using only size, marker and ori data:
::  size: 4245
+
:  size: 4245
::  marker: Tet, Cam
+
:  marker: Tet, Cam
::  ori: p15A
+
:  ori: p15A
  
 
* Confirm that your new list's structure looks the pUC19 one (minus "accession", and the "BanI"" element).
 
* 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:
 
* Make a new list, call it plasmidDB and assign to it the puc19 list:
<source lang="rsplus">
+
<pre>
 
plasmidDB <- list()
 
plasmidDB <- list()
 
plasmidDB[["pUC19"]] <- pUC19
 
plasmidDB[["pUC19"]] <- pUC19
</source>
+
</pre>
 
* Add your pACYC184 list
 
* Add your pACYC184 list
 
* Add a third element to plasmidDB, "pBR322" using the pBR322 data:
 
* Add a third element to plasmidDB, "pBR322" using the pBR322 data:
::  size: 4361
+
:  size: 4361
::  marker: Amp, Tet
+
:  marker: Amp, Tet
::  ori: ColE1
+
:  ori: ColE1
  
  
Line 136: Line 130:
 
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 <code>lapply()</code>, one of several "apply" functions. For example, to get all "ori" elements, try:
 
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 <code>lapply()</code>, one of several "apply" functions. For example, to get all "ori" elements, try:
  
<source lang="rsplus">
+
<pre>
 
lapply(plasmidDB, function(x) { return(x$ori) })
 
lapply(plasmidDB, function(x) { return(x$ori) })
</source>
+
</pre>
  
 
* Retrieve all sizes from the list. Use <code>unlist()</code> to flatten the result. Then use <code>min()</code> to find the size of the smallest one.
 
* Retrieve all sizes from the list. Use <code>unlist()</code> to flatten the result. Then use <code>min()</code> to find the size of the smallest one.
Line 145: Line 139:
 
Lost? Solutions here (but don't peek).
 
Lost? Solutions here (but don't peek).
 
<div class="mw-collapsible-content">
 
<div class="mw-collapsible-content">
<source lang="rsplus">
+
<pre>
 
pUC19 <- list(size=2686, marker="ampicillin", ori="ColE1", accession="L01397", BanI=c(235, 408, 550, 1647) )
 
pUC19 <- list(size=2686, marker="ampicillin", ori="ColE1", accession="L01397", BanI=c(235, 408, 550, 1647) )
 
pACYC184 <- list(size=4245, marker="Tet, Cam", ori="p15A" )
 
pACYC184 <- list(size=4245, marker="Tet, Cam", ori="p15A" )
Line 161: Line 155:
 
# or, to get the name too ...
 
# or, to get the name too ...
 
x[x == min(x)]
 
x[x == min(x)]
</source>
+
</pre>
  
 
</div>
 
</div>
Line 173: Line 167:
 
{{Vspace}}
 
{{Vspace}}
  
 
{{Vspace}}
 
 
 
== Further reading, links and resources ==
 
<!-- {{#pmid: 19957275}} -->
 
<!-- {{WWW|WWW_GMOD}} -->
 
<!-- <div class="reference-box">[http://www.ncbi.nlm.nih.gov]</div> -->
 
 
{{Vspace}}
 
 
 
== Notes ==
 
<!-- included from "./components/RPR-Objects-Lists.components.txt", section: "notes" -->
 
<!-- included from "./data/ABC-unit_components.txt", section: "notes" -->
 
<references />
 
 
{{Vspace}}
 
 
 
</div>
 
<div id="ABC-unit-framework">
 
 
== Self-evaluation ==
 
== Self-evaluation ==
<!-- included from "./components/RPR-Objects-Lists.components.txt", section: "self-evaluation" -->
 
  
 
=== Question 1===
 
=== Question 1===
Line 220: Line 191:
 
{{Vspace}}
 
{{Vspace}}
  
 
 
{{Vspace}}
 
 
 
<!-- included from "./data/ABC-unit_components.txt", section: "ABC-unit_ask" -->
 
 
----
 
 
{{Vspace}}
 
 
<b>If in doubt, ask!</b> 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.
 
 
----
 
 
{{Vspace}}
 
  
 
<div class="about">
 
<div class="about">
Line 245: Line 200:
 
:2017-08-05
 
:2017-08-05
 
<b>Modified:</b><br />
 
<b>Modified:</b><br />
:2017-09-24
+
:2020-09-17
 
<b>Version:</b><br />
 
<b>Version:</b><br />
:1.0.1
+
:1.0.3
 
<b>Version history:</b><br />
 
<b>Version history:</b><br />
 +
*1.0.3  Maintenace
 +
*1.0.2  Maintenace
 
*1.0.1  Fixed error in list example
 
*1.0.1  Fixed error in list example
 
*1.0    Completed to first live version
 
*1.0    Completed to first live version
 
*0.1    Material collected from previous tutorial
 
*0.1    Material collected from previous tutorial
 
</div>
 
</div>
[[Category:ABC-units]]
 
<!-- included from "./data/ABC-unit_components.txt", section: "ABC-unit_footer" -->
 
  
 
{{CC-BY}}
 
{{CC-BY}}
  
 +
[[Category:ABC-units]]
 +
{{UNIT}}
 +
{{LIVE}}
 
</div>
 
</div>
 
<!-- [END] -->
 
<!-- [END] -->

Latest revision as of 01:07, 6 September 2021

R "Lists"

(R Lists)


 


Abstract:

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


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.

Prerequisites:


 



 



 


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 our data.frame() example, we stored multiple restriction enzymes in one string, separated by commas. 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:

  • Create a list like the one above 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

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:

lapply(plasmidDB, function(x) { return(x$ori) })
  • Retrieve all sizes from the list. Use unlist() to flatten the result. Then use min() to find the size of the smallest one.

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

pUC19 <- list(size=2686, marker="ampicillin", ori="ColE1", accession="L01397", BanI=c(235, 408, 550, 1647) )
pACYC184 <- list(size=4245, marker="Tet, Cam", ori="p15A" )

plasmidDB <- list()
plasmidDB[["pUC19"]] <- pUC19
plasmidDB[["pACYC184"]] <- pACYC184
plasmidDB[["pBR322"]] <- list(size=4361, marker="Amp, Tet", ori="ColE1" )

plasmidDB[["pACYC184"]] # retrieve the entire pACYC184 list

lapply(plasmidDB, function(x) { return(x$ori) })
x <- unlist(lapply(plasmidDB, function(x) { return(x$size) }))
min(x)
# or, to get the name too ...
x[x == min(x)]



 

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.


 


 


About ...
 
Author:

Boris Steipe <boris.steipe@utoronto.ca>

Created:

2017-08-05

Modified:

2020-09-17

Version:

1.0.3

Version history:

  • 1.0.3 Maintenace
  • 1.0.2 Maintenace
  • 1.0.1 Fixed error in list example
  • 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.