Difference between revisions of "RPR-Subsetting"

From "A B C"
Jump to navigation Jump to search
m
m
 
(9 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%;">
 
Subsetting and filtering R objects
 
Subsetting and filtering R objects
  </div>
+
<div style="padding:5px; margin-top:20px; margin-bottom:10px; background-color:#b3dbce; font-size:30%; font-weight:200; color: #000000; ">
 
+
(Subsetting with the [], [[]], and $ operators, filtering)
  {{Vspace}}
+
</div>
 
 
<div class="keywords">
 
<b>Keywords:</b>&nbsp;
 
Subsetting with the [], [[]], and $ operators, filtering
 
 
</div>
 
</div>
  
{{Vspace}}
+
{{Smallvspace}}
 
 
 
 
__TOC__
 
 
 
{{Vspace}}
 
 
 
  
{{LIVE}}
 
  
{{Vspace}}
+
<div style="padding:5px; border:1px solid #000000; background-color:#b3dbce33; font-size:85%;">
 
+
<div style="font-size:118%;">
 
+
<b>Abstract:</b><br />
</div>
 
<div id="ABC-unit-framework">
 
== Abstract ==
 
 
<section begin=abstract />
 
<section begin=abstract />
<!-- included from "../components/RPR-Subsetting.components.wtxt", section: "abstract" -->
+
Subsetting and filtering are among the most important operations with data. R provides powerful syntax for these operations. Learn about and practice them in this unit.
...
 
 
<section end=abstract />
 
<section end=abstract />
 
+
</div>
{{Vspace}}
+
<!-- ============================ -->
 
+
<hr>
 
+
<table>
== This unit ... ==
+
<tr>
=== Prerequisites ===
+
<td style="padding:10px;">
<!-- included from "../components/RPR-Subsetting.components.wtxt", section: "prerequisites" -->
+
<b>Objectives:</b><br />
<!-- included from "ABC-unit_components.wtxt", section: "notes-prerequisites" -->
 
You need to complete the following units before beginning this one:
 
*[[RPR-Objects-Lists]]
 
 
 
{{Vspace}}
 
 
 
 
 
=== Objectives ===
 
<!-- included from "../components/RPR-Subsetting.components.wtxt", section: "objectives" -->
 
 
This unit will ...
 
This unit will ...
* ... introduce ;
+
* ... introduce subsetting principles;
* ... discuss ;
+
* ... practice them on data;
* ... teach ;
+
</td>
 +
<td style="padding:10px;">
 +
<b>Outcomes:</b><br />
 +
After working through this unit you ...
 +
* ... can subset and filter data according to six different principles.
 +
</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 />
 +
This unit builds on material covered in the following prerequisite units:<br />
 +
*[[RPR-Objects-Lists|RPR-Objects-Lists (R "Lists")]]
 +
<section end=prerequisites />
 +
<!-- ============================  -->
 +
</div>
  
{{Vspace}}
+
{{Smallvspace}}
  
  
=== Outcomes ===
 
<!-- included from "../components/RPR-Subsetting.components.wtxt", section: "outcomes" -->
 
After working through this unit you ...
 
* ... have done;
 
* ... know how ;
 
* ... can ;
 
  
{{Vspace}}
+
{{Smallvspace}}
  
  
=== Deliverables ===
+
__TOC__
<!-- included from "../components/RPR-Subsetting.components.wtxt", section: "deliverables" -->
 
<!-- included from "ABC-unit_components.wtxt", 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 "ABC-unit_components.wtxt", 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 "ABC-unit_components.wtxt", 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}}
Line 78: Line 67:
  
 
=== Evaluation ===
 
=== Evaluation ===
<!-- included from "../components/RPR-Subsetting.components.wtxt", section: "evaluation" -->
 
<!-- included from "ABC-unit_components.wtxt", section: "eval-none" -->
 
 
<b>Evaluation: NA</b><br />
 
<b>Evaluation: NA</b><br />
:This unit is not evaluated for course marks.
+
<div style="margin-left: 2rem;">This unit is not evaluated for course marks.</div>
 +
== Contents ==
  
{{Vspace}}
+
{{task| 1=
 +
* Load the <code>R-Exercise_BasicSetup</code> project in RStudio if you don't already have it open.
 +
* Type <code>init()</code> as instructed after the project has loaded.
 +
* Recreate the <code>plasmidData</code> data frame that you worked with in the [[RPR-Objects-Data_frames]] unit, (if it is not still defined in your Workspace; or use the code below):
 +
<pre>
 +
plasmidData <- data.frame(Name = c("pUC19", "pBR322", "pACYC184", "pMAL-p5x"),
 +
                          Size = c(2686, 4361, 4245, 5752),
 +
                          Marker = c("Amp", "Amp, Tet", "Cam", "Amp"),
 +
                          Ori = c("ColE1", "ColE1", "p15A", "pMB1"),
 +
                          Sites = c("EcoRI, SacI, SmaI, BamHI, HindIII",
 +
                                    "EcoRI, ClaI, HindIII",
 +
                                    "ClaI, HindIII",
 +
                                    "SacI, AvaI, HindIII"))
 +
</pre>
 +
* Continue below.
 +
}}
  
  
</div>
 
<div id="BIO">
 
== Contents ==
 
<!-- included from "../components/RPR-Subsetting.components.wtxt", section: "contents" -->
 
 
===Subsetting===
 
===Subsetting===
  
 +
We have encountered "subsetting" before, but we really need to discuss this in more detail. It is one of the most important topics of '''R''' since it is indispensable to select, transform, and otherwise modify data to prepare it for analysis. You have seen that we use square brackets to indicate individual elements in vectors and matrices. These square brackets are actually "operators", and you can find more information about them in the help  pages:
  
We have encountered "subsetting" before, but we really need to discuss this in more detail. It is one of the most important and powerful topics of '''R''' since it is indispensable to select, transform, and otherwise modify data to prepare it for analysis. You have seen that we use square brackets to indicate individual elements in vectors and matrices. These square brackets are actually "operators", and you can find more information about them in the help  pages:
+
<pre>
 
 
<source lang="rsplus">
 
 
> ?"["    # Note that you need quotation marks around the operator for this.
 
> ?"["    # Note that you need quotation marks around the operator for this.
</source>
+
</pre>
  
 
Note especially:
 
Note especially:
* <code>[ ]</code> "extracts" one or more elements defined within the brackets;
+
- <code>[ ]</code> "extracts" one or more elements defined within the brackets;
* <code>[[ ]]</code> "extracts" a single element defined within the brackets;
+
- <code>[[ ]]</code> "extracts" a single element defined within the brackets;
* <code>?</code> "extracts" a single <u>named</u> element that.
+
- <code>$</code> "extracts" a single <u>named</u> element.
  
 
"Elements" are not necessarily scalars, but can apply to a row, column, or more complex data structure. But a "single element" can't be a range, or collection.
 
"Elements" are not necessarily scalars, but can apply to a row, column, or more complex data structure. But a "single element" can't be a range, or collection.
  
 +
{{Vspace}}
  
 +
Here are some examples of subsetting data from the <code>plasmidData</code> data frame we constructed previously. For the most part, this is review:
  
Here are some examples of subsetting data from the <code>plasmidData</code> data frame we constructed above. For the most part, this is review of what we already did above:
+
<pre>
 
 
<source lang="rsplus">
 
 
plasmidData[1, ]
 
plasmidData[1, ]
 
plasmidData[2, ]
 
plasmidData[2, ]
Line 136: Line 134:
 
plasmidData$Name[plasmidData$Ori != "ColE1"]
 
plasmidData$Name[plasmidData$Ori != "ColE1"]
 
# What happened here?
 
# What happened here?
# plasmidData$Ori != "ColE1" is a logical expression, it gives a vector of TRUE/FALSE values
+
# plasmidData$Ori != "ColE1" is a logical expression, it gives a vector of TRUE/FALSE values:
 
plasmidData$Ori != "ColE1"
 
plasmidData$Ori != "ColE1"
  
# We insert this vector into the square brackets. R then returns all rows for
+
# ... insert this vector into the square brackets. R then returns all rows for
# which the vector is TRUE.
+
# which the corresponding vector element is TRUE.
  
# In this way we can "filter" for values
+
# With this, we can "filter" for values
 
plasmidData$Size > 3000
 
plasmidData$Size > 3000
 
plasmidData$Name[plasmidData$Size > 3000]
 
plasmidData$Name[plasmidData$Size > 3000]
 +
 +
# Any operation that has TRUE or FALSE as a result can be used for filtering:
 +
#  - the equality operators == and !=
 +
#  - the comparison operators  >, <, >=, and <=
 +
#  - %in%
 +
#  - grepl()
 +
#  - as.logical()
 +
 +
# plasmids that have only the Amp marker
 +
plasmidData[plasmidData$Marker == "Amp"]
 +
 +
# plasmids that don't have the Amp marker
 +
plasmidData[plasmidData$Marker == "Amp"]
 +
 +
  
 
# This principle is what we use when we want to "sort" an object
 
# This principle is what we use when we want to "sort" an object
Line 157: Line 170:
 
plasmidData[grep("Tet", plasmidData$Marker), "Ori"]
 
plasmidData[grep("Tet", plasmidData$Marker), "Ori"]
  
</source>
+
</pre>
  
 
Elements that can be extracted from an object also can be replaced. Simply assign the new value to the element.
 
Elements that can be extracted from an object also can be replaced. Simply assign the new value to the element.
  
<source lang="rsplus">
+
<pre>
 
( x <- sample(1:10) )
 
( x <- sample(1:10) )
 
x[4] <- 99
 
x[4] <- 99
Line 167: Line 180:
 
( x <- x[order(x)] )
 
( x <- x[order(x)] )
  
</source>
+
</pre>
  
Try your own subsetting ideas. Play with this. I find that even seasoned investigators have problems with subsetting their data and if you become comfortable with the many ways of subsetting, you will be ahaed of the game right away.
+
Try your own subsetting ideas. Play with this. I find that even seasoned investigators have problems with subsetting their data and if you become comfortable with the many ways of subsetting, you will be ahead of the game right away.
  
 
{{Vspace}}
 
{{Vspace}}
  
 +
===Subsetting practice===
  
{{Vspace}}
 
 
 
== Further reading, links and resources ==
 
<!-- {{#pmid: 19957275}} -->
 
<!-- {{WWW|WWW_GMOD}} -->
 
<!-- <div class="reference-box">[http://www.ncbi.nlm.nih.gov]</div> -->
 
  
{{Vspace}}
+
{{task|1=
 +
Practice, practice, [https://www.gocomics.com/sarahs-scribbles/2017/12/20 practice]. You need to develop an intuition about subsetting. This will help you tremendously to understand our code examples - but it will also help you develop ways to '''think''' about data.
  
 +
* The <code>R-Exercise_BasicSetup</code> project contains a file <code>subsettingPractice.R</code>
 +
* Open the file and work through it.
  
== Notes ==
+
}}
<!-- included from "../components/RPR-Subsetting.components.wtxt", section: "notes" -->
 
<!-- included from "ABC-unit_components.wtxt", section: "notes" -->
 
<references />
 
  
 
{{Vspace}}
 
{{Vspace}}
  
 
</div>
 
<div id="ABC-unit-framework">
 
== Self-evaluation ==
 
<!-- included from "../components/RPR-Subsetting.components.wtxt", section: "self-evaluation" -->
 
<!--
 
=== Question 1===
 
 
Question ...
 
 
<div class="toccolours mw-collapsible mw-collapsed" style="width:800px">
 
Answer ...
 
<div class="mw-collapsible-content">
 
Answer ...
 
 
</div>
 
  </div>
 
 
  {{Vspace}}
 
 
-->
 
 
{{Vspace}}
 
 
 
 
{{Vspace}}
 
 
 
<!-- included from "ABC-unit_components.wtxt", 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 241: Line 208:
 
:2017-08-05
 
:2017-08-05
 
<b>Modified:</b><br />
 
<b>Modified:</b><br />
:2017-09-10
+
:2020-09-18
 
<b>Version:</b><br />
 
<b>Version:</b><br />
:1.0
+
:1.0.2
 
<b>Version history:</b><br />
 
<b>Version history:</b><br />
 +
*1.0.2 Maintenance
 +
*1.0.1 Maintenance
 
*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 "ABC-unit_components.wtxt", section: "ABC-unit_footer" -->
 
  
 
{{CC-BY}}
 
{{CC-BY}}
  
 +
[[Category:ABC-units]]
 +
{{UNIT}}
 +
{{LIVE}}
 
</div>
 
</div>
 
<!-- [END] -->
 
<!-- [END] -->

Latest revision as of 09:29, 25 September 2020

Subsetting and filtering R objects

(Subsetting with the [], [[]], and $ operators, filtering)


 


Abstract:

Subsetting and filtering are among the most important operations with data. R provides powerful syntax for these operations. Learn about and practice them in this unit.


Objectives:
This unit will ...

  • ... introduce subsetting principles;
  • ... practice them on data;

Outcomes:
After working through this unit you ...

  • ... can subset and filter data according to six different principles.

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:
This unit builds on material covered in the following prerequisite units:


 



 



 


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.
  • Recreate the plasmidData data frame that you worked with in the RPR-Objects-Data_frames unit, (if it is not still defined in your Workspace; or use the code below):
plasmidData <- data.frame(Name = c("pUC19", "pBR322", "pACYC184", "pMAL-p5x"),
                          Size = c(2686, 4361, 4245, 5752),
                          Marker = c("Amp", "Amp, Tet", "Cam", "Amp"),
                          Ori = c("ColE1", "ColE1", "p15A", "pMB1"),
                          Sites = c("EcoRI, SacI, SmaI, BamHI, HindIII",
                                    "EcoRI, ClaI, HindIII",
                                    "ClaI, HindIII",
                                    "SacI, AvaI, HindIII"))
  • Continue below.


Subsetting

We have encountered "subsetting" before, but we really need to discuss this in more detail. It is one of the most important topics of R since it is indispensable to select, transform, and otherwise modify data to prepare it for analysis. You have seen that we use square brackets to indicate individual elements in vectors and matrices. These square brackets are actually "operators", and you can find more information about them in the help pages:

> ?"["     # Note that you need quotation marks around the operator for this.

Note especially: - [ ] "extracts" one or more elements defined within the brackets; - [[ ]] "extracts" a single element defined within the brackets; - $ "extracts" a single named element.

"Elements" are not necessarily scalars, but can apply to a row, column, or more complex data structure. But a "single element" can't be a range, or collection.


 

Here are some examples of subsetting data from the plasmidData data frame we constructed previously. For the most part, this is review:

plasmidData[1, ]
plasmidData[2, ]

# we can extract more than one row by specifying
# the rows we want in a vector ...
plasmidData[c(1, 2), ]

# ... this works in any order ...
plasmidData[c(3, 1), ]

# ... and for any number of rows ...
plasmidData[c(1, 2, 1, 2, 1, 2), ]


# Same for columns
plasmidData[ , 2 ]

# We can select rows and columns by name if a name has been defined...
plasmidData[, "Name"]
plasmidData$Name      # different syntax, same thing. This is the syntax I use most frequently.


# Watch this!
plasmidData$Name[plasmidData$Ori != "ColE1"]
# What happened here?
# plasmidData$Ori != "ColE1" is a logical expression, it gives a vector of TRUE/FALSE values:
plasmidData$Ori != "ColE1"

# ... insert this vector into the square brackets. R then returns all rows for
# which the corresponding vector element is TRUE.

# With this, we can "filter" for values
plasmidData$Size > 3000
plasmidData$Name[plasmidData$Size > 3000]

# Any operation that has TRUE or FALSE as a result can be used for filtering:
#   - the equality operators == and !=
#   - the comparison operators  >, <, >=, and <=
#   - %in%
#   - grepl()
#   - as.logical()

# plasmids that have only the Amp marker
plasmidData[plasmidData$Marker == "Amp"]

# plasmids that don't have the Amp marker
plasmidData[plasmidData$Marker == "Amp"]



# This principle is what we use when we want to "sort" an object
# by some value. The function order() is used to return values
# that are sorted. Remember this: not sort() but order().
order(plasmidData$Size)
plasmidData[order(plasmidData$Size), ]

# grep() matches substrings in strings and returns a vector of indices
grep("Tet", plasmidData$Marker)
plasmidData[grep("Tet", plasmidData$Marker), ]
plasmidData[grep("Tet", plasmidData$Marker), "Ori"]

Elements that can be extracted from an object also can be replaced. Simply assign the new value to the element.

( x <- sample(1:10) )
x[4] <- 99
x
( x <- x[order(x)] )

Try your own subsetting ideas. Play with this. I find that even seasoned investigators have problems with subsetting their data and if you become comfortable with the many ways of subsetting, you will be ahead of the game right away.


 

Subsetting practice

Task:
Practice, practice, practice. You need to develop an intuition about subsetting. This will help you tremendously to understand our code examples - but it will also help you develop ways to think about data.

  • The R-Exercise_BasicSetup project contains a file subsettingPractice.R
  • Open the file and work through it.


 


About ...
 
Author:

Boris Steipe <boris.steipe@utoronto.ca>

Created:

2017-08-05

Modified:

2020-09-18

Version:

1.0.2

Version history:

  • 1.0.2 Maintenance
  • 1.0.1 Maintenance
  • 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.