Difference between revisions of "RPR-OBJECTS-Data frames"

From "A B C"
Jump to navigation Jump to search
m
m
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 "data frames""
 
R "data frames""
  </div>
+
<div style="padding:5px; margin-top:20px; margin-bottom:10px; background-color:#b3dbce; font-size:30%; font-weight:200; color: #000000; ">
 
+
(R data frames)
  {{Vspace}}
+
</div>
 
 
<div class="keywords">
 
<b>Keywords:</b>&nbsp;
 
R data frames
 
 
</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-Objects-Data_frames.components.txt", section: "abstract" -->
 
 
Introduction to data frames: how to create, and modify them and how to retrieve data.
 
Introduction to data frames: 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-Data_frames.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-Vectors|RPR-Objects-Vectors (R scalars and vectors)]]
 
 
 
{{Vspace}}
 
 
 
 
 
=== Objectives ===
 
<!-- included from "./components/RPR-Objects-Data_frames.components.txt", section: "objectives" -->
 
 
This unit will ...
 
This unit will ...
 
* ... introduce R data frames;
 
* ... introduce R data frames;
 
* ... cover a number of basic operations.
 
* ... cover a number of basic operations.
 
+
</td>
{{Vspace}}
+
<td style="padding:10px;">
 
+
<b>Outcomes:</b><br />
 
 
=== Outcomes ===
 
<!-- included from "./components/RPR-Objects-Data_frames.components.txt", section: "outcomes" -->
 
 
After working through this unit you ...
 
After working through this unit you ...
 
* ... know how to create and manipulate data frames;
 
* ... know how to create and manipulate data frames;
 
* ... can extract rows, columns, and append new data rows;
 
* ... can extract rows, columns, and append new data rows;
 
+
</td>
{{Vspace}}
+
</tr>
 
+
</table>
 
+
<!-- ============================ -->
=== Deliverables ===
+
<hr>
<!-- included from "./components/RPR-Objects-Data_frames.components.txt", section: "deliverables" -->
+
<b>Deliverables:</b><br />
 +
<section begin=deliverables />
 
<!-- included from "./data/ABC-unit_components.txt", section: "deliverables-time_management" -->
 
<!-- 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.
Line 71: Line 45:
 
<!-- included from "./data/ABC-unit_components.txt", section: "deliverables-insights" -->
 
<!-- 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]].
 +
<section end=deliverables />
 +
<!-- ============================  -->
 +
<hr>
 +
<section begin=prerequisites />
 +
<b>Prerequisites:</b><br />
 +
<!-- included from "./data/ABC-unit_components.txt", section: "notes-prerequisites" -->
 +
This unit builds on material covered in the following prerequisite units:
 +
*[[RPR-Objects-Vectors|RPR-Objects-Vectors (R scalars and vectors)]]
 +
<section end=prerequisites />
 +
<!-- ============================  -->
 +
</div>
 +
 +
{{Smallvspace}}
 +
 +
 +
 +
{{Smallvspace}}
 +
 +
 +
__TOC__
  
 
{{Vspace}}
 
{{Vspace}}
  
  
</div>
 
<div id="BIO">
 
 
== Contents ==
 
== Contents ==
 
<!-- included from "./components/RPR-Objects-Data_frames.components.txt", section: "contents" -->
 
<!-- included from "./components/RPR-Objects-Data_frames.components.txt", section: "contents" -->
Line 159: Line 151:
  
  
 
{{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-Data_frames.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-Data_frames.components.txt", section: "self-evaluation" -->
 
 
<!--
 
<!--
 
=== Question 1===
 
=== Question 1===
Line 199: Line 168:
  
 
-->
 
-->
 
+
== Notes ==
{{Vspace}}
+
<!-- included from "./components/RPR-Objects-Data_frames.components.txt", section: "notes" -->
 
+
<!-- included from "./data/ABC-unit_components.txt", section: "notes" -->
 
+
<references />
 +
== Further reading, links and resources ==
 +
<!-- {{#pmid: 19957275}} -->
 +
<!-- {{WWW|WWW_GMOD}} -->
 +
<!-- <div class="reference-box">[http://www.ncbi.nlm.nih.gov]</div> -->
  
 
{{Vspace}}
 
{{Vspace}}

Revision as of 19:32, 26 January 2018

R "data frames""

(R data frames)


 


Abstract:

Introduction to data frames: how to create, and modify them and how to retrieve data.


Objectives:
This unit will ...

  • ... introduce R data frames;
  • ... cover a number of basic operations.

Outcomes:
After working through this unit you ...

  • ... know how to create and manipulate data frames;
  • ... can extract rows, columns, and append new data rows;

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:


 



 



 


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.


Data frames

Data frames are probably the most important type of data object for bioinformatics in R; they emulate our mental model of data in a spreadsheet and can be used to implement datamodels.

Usually the result of reading external data from an input file is a data frame. The file below is included with the R-Exercise-BasicSetup project files - it is called plasmidData.tsv, and you can click on it in the Files Pane to open and inspect it.

Name	Size	Marker	Ori	Sites
pUC19	2686	Amp	ColE1	EcoRI, SacI, SmaI, BamHI, XbaI, PstI, HindIII
pBR322	4361	Amp, Tet	ColE1	EcoRI, ClaI, HindIII
pACYC184	4245	Tet, Cam	p15A	ClaI, HindIII

This data set uses tabs as column separators and it has a header line. Similar files can be exported from Excel or other spreadsheet programs. Read this as a data frame as follows:

plasmidData <- read.table("plasmidData.tsv", sep="\t", header=TRUE, stringsAsFactors = FALSE)
plasmidData   # show what the data frame contains
objectInfo(plasmidData)

Note the argument stringsAsFactors = FALSE. If this is TRUE instead, R will convert all strings in the input to factors and this may lead to problems. Make it a habit to turn this behaviour off, you can always turn a column of strings into factors when you actually mean to have factors.

You can view the data frame contents by clicking on the spreadsheet icon behind its name in the Environment Pane.

Basic operations

Here are some basic operations with the data frame. Try them and experiment. If you break it by mistake, you can just recreate it by reading the source file again:

rownames(plasmidData) <- plasmidData[ , 1]  # use column 1 as rownames
nrow(plasmidData)
ncol(plasmidData)
objectInfo(plasmidData)


x <- plasmidData[2, ]  # assign one row to a variable
objectInfo(x)  # This is also a data frame! One row. It has to be, because
               # it contains elements of type chr and of type int!

plasmidData["pBR322", ]  # retrieve one row: different syntax, same thing

plasmidData[ , 2]       # retrieve one column
plasmidData[ , "Size"]  # retrieve one column: same principle



plasmidData <- plasmidData[-2, ]  # remove one row
objectInfo(plasmidData)

plasmidData <- rbind(plasmidData, x)  # add it back at the end
objectInfo(plasmidData)

# add a new row from scratch:
plasmidData <- rbind(plasmidData, data.frame(Name = "pMAL-p5x",
                                                     Size = 5752,
                                                     Marker = "Amp",
                                                     Ori = "pMB1",
                                                     Sites = "SacI, AvaI, ..., HindIII",
                                                     stringsAsFactors = FALSE))
objectInfo(plasmidData)

Task:
The rowname of the new row of plasmidData is now "1". It should be "pMAL-p5x". Fix this.


 


Self-evaluation

Notes

Further reading, links and resources

 




 

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.