-
Notifications
You must be signed in to change notification settings - Fork 51
/
R_tutorial.html
356 lines (311 loc) · 14.1 KB
/
R_tutorial.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<title>Chapter X: R tutorial</title>
<script src="libs/jquery-1.11.0/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="libs/bootstrap-3.3.1/css/readable.min.css" rel="stylesheet" />
<script src="libs/bootstrap-3.3.1/js/bootstrap.min.js"></script>
<script src="libs/bootstrap-3.3.1/shim/html5shiv.min.js"></script>
<script src="libs/bootstrap-3.3.1/shim/respond.min.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-52691601-2', 'auto');
ga('send', 'pageview');
</script>
<!---
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 50px;
padding-bottom: 40px;
padding-right: 50px;
padding-left: -50px;
}
@media (max-width: 979px) {
body {
padding-top: 0;
}
}
offset scroll position for anchor links (for fixed navbar)
@media (min-width: 980px) {
.section h2 {
padding-top: 52px;
margin-top: -52px;
}
.section h3 {
padding-top: 52px;
margin-top: -52px;
}
}
/* don't use link color in navbar */
.dropdown-menu>li>a {
color: black;
}
</style>
-->
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 50px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h2 {
padding-top: 55px;
margin-top: -55px;
}
.section h3 {
padding-top: 55px;
margin-top: -55px;
}
/* don't use link color in navbar */
.dropdown-menu>li>a {
color: black;
}
/* some padding for disqus */
#disqus_thread {
margin-top: 45px;
}
</style>
<link rel="stylesheet" href="libs/font-awesome-4.1.0/css/font-awesome.min.css"/>
<style type="text/css">code{white-space: pre;}</style>
<link rel="stylesheet"
href="libs/highlight/default.css"
type="text/css" />
<script src="libs/highlight/highlight.js"></script>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs && document.readyState && document.readyState === "complete") {
window.setTimeout(function() {
hljs.initHighlighting();
}, 0);
}
</script>
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
</style>
<div class="container-fluid main-container">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- HOME -->
<a class="navbar-brand" href="index.html"><b>Population Genetics in R</b></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<!-- Table of contents -->
<li class="dropdown">
<!---<a href="chapters" class="dropdown-toggle" data-toggle="dropdown">Chapters<b class="caret"></b></a>-->
<a href="chapters" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Chapters <span class="caret"></span></a>
<ol class="dropdown-menu">
<li><a href="Preface.html">i. Preface</a></li>
<li><a href="Introduction.html">1. Introduction</a></li>
<li><a href="Getting_ready_to_use_R.html">2. Getting ready to use R</a></li>
<li><a href="Data_Preparation.html">3. Data preparation</a></li>
<li><a href="First_Steps.html">4. First steps</a></li>
<li><a href="Population_Hierarchies.html">5. Population hierarchies and clone correction</a></li>
<li><a href="Locus_Stats.html">6. Locus-based statistics and missing data</a></li>
<li><a href="Genotypic_EvenRichDiv.html">7. Genotypic evenness, richness, and diversity</a></li>
<li><a href="Linkage_disequilibrium.html">8. Linkage disequilibrium</a></li>
<li><a href="Pop_Structure.html">9. Population structure</a></li>
<li><a href="AMOVA.html">10. AMOVA</a></li>
<li><a href="DAPC.html">11. Discriminant analysis of principal components (DAPC)</a></li>
</ol>
</li>
<!-- Appendices dropdown -->
<li class="dropdown">
<!---<a href="appendices" class="dropdown-toggle" data-toggle="dropdown">Appendices<b class="caret"></b></a>-->
<a href="appendices" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Appendices <span class="caret"></span></a>
<ul class = "dropdown-menu", role = "menu">
<li><a href="Data_sets.html">A1. Data sets</a></li>
<li><a href="Function_Glossary.html">A2. Function glossary</a></li>
</ul>
</li>
<!-- Author button -->
<li class="dropdown">
<a href="about" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">About<span class="caret"></span></a>
<ul class = "dropdown-menu", role = "menu">
<li><a href="Authors.html">Authors</a></li>
<li>
<a href="https://github.com/grunwaldlab/Population_Genetics_in_R", target = "blank">
Source code
</a>
</li>
</ul>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<!--
old rmarkdown version:
devtools::install_github("rstudio/rmarkdown", ref = "b96214b9ac86b437067a0aa21442203f52face83")
-->
<!-- <div class="container">
<div class="row">
<div class="span10 offset1"> -->
<div id="header">
<h1 class="title">Chapter X: R tutorial</h1>
</div>
<div id="TOC">
<ul>
<li><a href="#assignment-data-types-and-operations">Assignment, data types and operations</a></li>
<li><a href="#console-tricks-code-completion-command-history">Console tricks: Code completion, command history,</a></li>
<li><a href="#useful-r-functions">Useful R functions</a></li>
<li><a href="#getting-help">Getting help</a></li>
<li><a href="#getting-data-into-r">Getting data into R</a></li>
<li><a href="#basic-analysis">Basic analysis</a></li>
<li><a href="#basic-graphing">Basic graphing</a></li>
<li><a href="#creating-lab-reports-and-notebooks-of-your-analyses-using-r-markdown">Creating lab reports and notebooks of your analyses using R markdown</a></li>
<li><a href="#useful-resources">Useful resources</a></li>
<li><a href="#references">References</a></li>
</ul>
</div>
<p>This chapter introduces basic use of the <a href="http://www.r-project.org">R</a> statistical and computer language and the <a href="http://www.rstudio.com/ide/">Rstudio</a> graphics user interface. We assume you installed the needed resources mentioned in chapter 2 on getting ready to use R.</p>
<blockquote>
<p>You can skip this chapter if you have worked in R before and are comfortable using Rstudio</p>
</blockquote>
<p>After installing both R and R studio, please proceed to open R studio. R studio should look something like:</p>
<p><img src="images/Rstudio.png" alt="Rstudio" /></p>
<p>The top left panel is a text-box where you can write a script. The bottom left panel is the console (or command line) where you directly execute commands and R writes results to the console; for example, type in <code>x <- 1</code> hit return and then type <code>x</code> and hit return again and watch the console:</p>
<pre class="r"><code>x <- 1
x</code></pre>
<pre><code>## [1] 1</code></pre>
<p>You just assigned the value 1 to the variable <code>x</code> with the assignment operator <code><-</code>. The second command asks R to show the value assigned to R. Finally, the top right panel shows variables loaded into the environment and the bottom right panel is useful for loading files, plotting graphs, installing packages and getting help. Feel free to explore all the various tabs or elements of each panel.</p>
<div id="assignment-data-types-and-operations" class="section level2">
<h2>Assignment, data types and operations</h2>
<p>Let’s learn a few simple facts about R:</p>
<p>R uses <code>#</code> to add comments to code:</p>
<pre class="r"><code># Add 1 + 1 and write to console
1 + 1</code></pre>
<pre><code>## [1] 2</code></pre>
<p>This is very useful when developing reusable code or when you want to remember a few months later what you actually did.</p>
<p>R is case sensitive so that <code>x</code> is not the same as <code>X</code>. Try typing this into your console and look at <code>x</code> and <code>X</code>, respectively:</p>
<pre class="r"><code>x <- 5
X <- x + 1
x</code></pre>
<pre><code>## [1] 5</code></pre>
<pre class="r"><code>X</code></pre>
<pre><code>## [1] 6</code></pre>
<p>Next, let’s assign numbers to a vector:</p>
<pre class="r"><code>x <- c(1, 2, 3, 4, 5)
x</code></pre>
<pre><code>## [1] 1 2 3 4 5</code></pre>
<p>You can see several important traits of R. The <code>c()</code> command (which stands for combine) can be used to make a list (e.g., a vector) of data. We can overwrite the variable <code>x</code> with new content and we can assign another datatype (e.g., a single number becomes a vector). To compound things further you can even change the data type to strings overwriting the previous content:</p>
<pre class="r"><code>a <- c("Paris", "Tokyo", "Seattle")
a</code></pre>
<pre><code>## [1] "Paris" "Tokyo" "Seattle"</code></pre>
<p>Note that we use quotes to denote the string data type containing text.</p>
<p>To access individual elements of a vector, in this case the second element, we execute:</p>
<pre class="r"><code>a[2]</code></pre>
<pre><code>## [1] "Tokyo"</code></pre>
</div>
<div id="console-tricks-code-completion-command-history" class="section level2">
<h2>Console tricks: Code completion, command history,</h2>
<p>R studio has a very useful feature called code completion using the <strong>Tab</strong> key which can complete the full name of an object. For example type <code>hel</code> and hit <strong>Tab</strong> and you will see several functions pop up and you can select <code>help()</code>.</p>
<p>This also work inside a function to find function arguments. Type <code>help(</code> and hit <strong>Tab</strong> to select arguments for the help function.</p>
<p>R studio records your command history. Thus you can scroll up or down the history of executed commands using the <strong>Up</strong> or <strong>Down</strong> arrows. Make sure your cursor is in the console and try to re-execute previous commands.</p>
</div>
<div id="useful-r-functions" class="section level2">
<h2>Useful R functions</h2>
<p>Other useful commands providing information on your system include:</p>
<pre class="r"><code>help(options) # learn about available options
installed.packages()[,c("Package","Version")] # list packages & versions installed
sessionInfo() # displays packages currently loaded
data(<name>, package = "package") # load a specific data set from a specific package</code></pre>
<p>To quit R you can either use the <code>Rstudio > Quit</code> pull-down menu command or execute <code>⌘ + Q</code> (OS X) or <code>CTRL + Q</code> (PC).</p>
</div>
<div id="getting-help" class="section level2">
<h2>Getting help</h2>
</div>
<div id="getting-data-into-r" class="section level2">
<h2>Getting data into R</h2>
</div>
<div id="basic-analysis" class="section level2">
<h2>Basic analysis</h2>
</div>
<div id="basic-graphing" class="section level2">
<h2>Basic graphing</h2>
</div>
<div id="creating-lab-reports-and-notebooks-of-your-analyses-using-r-markdown" class="section level2">
<h2>Creating lab reports and notebooks of your analyses using R markdown</h2>
</div>
<div id="useful-resources" class="section level2">
<h2>Useful resources</h2>
<p><em>Introductory</em></p>
<ul>
<li><a href="http://swirlstats.com/">Swirl</a> is a very well thought out R package that teaches you interactively.</li>
<li><a href="https://www.codeschool.com/courses/try-r">Code School Try R</a> is a nice interactive tutorial.</li>
<li><a href="http://www.statmethods.net/interface/help.html">Quick R</a></li>
<li><a href="http://cran.r-project.org/doc/contrib/Short-refcard.pdf">R reference card</a></li>
<li>A very nice, short <a href="http://ateucher.github.io/rcourse_site/index.html">introduction to R</a></li>
</ul>
<p><em>Advanced</em></p>
<ul>
<li><a href="http://adv-r.had.co.nz/">Advanced R</a> by Hadley Wickham</li>
</ul>
<p><em>Books</em>:</p>
<ul>
<li><a href="http://oreilly.com/catalog/9780596801717">R in a Nutshell</a></li>
<li><a href="">R cookbook</a> is a nice quick reference and tutorial for general R use.</li>
<li><a href="">ggplot2: Elegant Graphics for Data Analysis</a> is a useful, bu dated reference if you want to customize graphs for publication.</li>
</ul>
</div>
<div id="references" class="section level2">
<h2>References</h2>
<!---- Reference list ---->
<div class="references">
</div>
</div>
<!-- </div>
</div>
</div>
-->
</div>
<script>
// add bootstrap table styles to pandoc tables
$(document).ready(function () {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>