Difference between revisions of "RPR-Subsetting"
m |
m |
||
Line 28: | Line 28: | ||
== Abstract == | == Abstract == | ||
<section begin=abstract /> | <section begin=abstract /> | ||
− | <!-- included from " | + | <!-- included from "./components/RPR-Subsetting.components.txt", 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. | 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 /> | ||
Line 37: | Line 37: | ||
== This unit ... == | == This unit ... == | ||
=== Prerequisites === | === Prerequisites === | ||
− | <!-- included from " | + | <!-- included from "./components/RPR-Subsetting.components.txt", section: "prerequisites" --> |
− | <!-- included from "ABC-unit_components. | + | <!-- included from "./data/ABC-unit_components.txt", section: "notes-prerequisites" --> |
You need to complete the following units before beginning this one: | You need to complete the following units before beginning this one: | ||
− | *[[RPR-Objects-Lists]] | + | *[[RPR-Objects-Lists|RPR-Objects-Lists (R "Lists")]] |
{{Vspace}} | {{Vspace}} | ||
Line 46: | Line 46: | ||
=== Objectives === | === Objectives === | ||
− | <!-- included from " | + | <!-- included from "./components/RPR-Subsetting.components.txt", section: "objectives" --> |
This unit will ... | This unit will ... | ||
* ... introduce subsetting principles; | * ... introduce subsetting principles; | ||
Line 55: | Line 55: | ||
=== Outcomes === | === Outcomes === | ||
− | <!-- included from " | + | <!-- included from "./components/RPR-Subsetting.components.txt", section: "outcomes" --> |
After working through this unit you ... | After working through this unit you ... | ||
* ... can subset and filter data according to six different principles. | * ... can subset and filter data according to six different principles. | ||
Line 63: | Line 63: | ||
=== Deliverables === | === Deliverables === | ||
− | <!-- included from " | + | <!-- included from "./components/RPR-Subsetting.components.txt", section: "deliverables" --> |
− | <!-- included from "ABC-unit_components. | + | <!-- 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. | *<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. | + | <!-- 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. | *<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. | + | <!-- 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]]. | *<b>Insights</b>: If you find something particularly noteworthy about this unit, make a note in your [[ABC-Insights|'''insights!''' page]]. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{{Vspace}} | {{Vspace}} | ||
Line 86: | Line 77: | ||
<div id="BIO"> | <div id="BIO"> | ||
== Contents == | == Contents == | ||
− | <!-- included from " | + | <!-- included from "./components/RPR-Subsetting.components.txt", section: "contents" --> |
{{task| 1= | {{task| 1= | ||
Line 200: | Line 191: | ||
== Notes == | == Notes == | ||
− | <!-- included from " | + | <!-- included from "./components/RPR-Subsetting.components.txt", section: "notes" --> |
− | <!-- included from "ABC-unit_components. | + | <!-- included from "./data/ABC-unit_components.txt", section: "notes" --> |
<references /> | <references /> | ||
Line 210: | Line 201: | ||
<div id="ABC-unit-framework"> | <div id="ABC-unit-framework"> | ||
== Self-evaluation == | == Self-evaluation == | ||
− | <!-- included from " | + | <!-- included from "./components/RPR-Subsetting.components.txt", section: "self-evaluation" --> |
<!-- | <!-- | ||
=== Question 1=== | === Question 1=== | ||
Line 235: | Line 226: | ||
− | <!-- included from "ABC-unit_components. | + | <!-- included from "./data/ABC-unit_components.txt", section: "ABC-unit_ask" --> |
---- | ---- | ||
Line 263: | Line 254: | ||
</div> | </div> | ||
[[Category:ABC-units]] | [[Category:ABC-units]] | ||
− | <!-- included from "ABC-unit_components. | + | <!-- included from "./data/ABC-unit_components.txt", section: "ABC-unit_footer" --> |
{{CC-BY}} | {{CC-BY}} |
Revision as of 01:27, 6 January 2018
Subsetting and filtering R objects
Keywords: Subsetting with the [], [[]], and $ operators, filtering
Contents
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.
This unit ...
Prerequisites
You need to complete the following units before beginning this one:
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.
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, if it is not still defined in your Workspace. - 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"
# We insert this vector into the square brackets. R then returns all rows for
# which the vector is TRUE.
# In this way we can "filter" for values
plasmidData$Size > 3000
plasmidData$Name[plasmidData$Size > 3000]
# 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 ahaed of the game right away.
Subsetting practice
Task:
- The
R-Exercise_BasicSetup
project contains a filesubsettingPractice.R
- Open the file and work through it.
Further reading, links and resources
Notes
Self-evaluation
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
This copyrighted material is licensed under a Creative Commons Attribution 4.0 International License. Follow the link to learn more.