forked from datacarpentry/R-genomics
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path01-intro-to-R.html
552 lines (479 loc) · 24.7 KB
/
01-intro-to-R.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<!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" />
<meta name="author" content="Data Carpentry contributors" />
<title>Introduction to R</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/font-awesome-4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #40a070; }
code > span.fl { color: #40a070; }
code > span.ch { color: #4070a0; }
code > span.st { color: #4070a0; }
code > span.co { color: #60a0b0; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #06287e; }
code > span.er { color: #ff0000; font-weight: bold; }
</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</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;
}
.tabbed-pane {
padding-top: 12px;
}
button.code-folding-btn:focus {
outline: none;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 51px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 56px;
margin-top: -56px;
}
.section h2 {
padding-top: 56px;
margin-top: -56px;
}
.section h3 {
padding-top: 56px;
margin-top: -56px;
}
.section h4 {
padding-top: 56px;
margin-top: -56px;
}
.section h5 {
padding-top: 56px;
margin-top: -56px;
}
.section h6 {
padding-top: 56px;
margin-top: -56px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<div class="container-fluid main-container">
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_').toLowerCase();
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = false;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
padding-left: 25px;
text-indent: 0;
}
.tocify .list-group-item {
border-radius: 0px;
}
.tocify-subheader {
display: inline;
}
.tocify-subheader .tocify-item {
font-size: 0.95em;
}
</style>
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<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>
<a class="navbar-brand" href="index.html"></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="00-before-we-start.html">Before we start</a>
</li>
<li>
<a href="01-intro-to-R.html">Intro to R</a>
</li>
<li>
<a href="02-starting-with-data.html">Starting with data</a>
</li>
<li>
<a href="03-data-frames.html">Data frames</a>
</li>
<li>
<a href="04-dplyr.html">The dplyr package</a>
</li>
<li>
<a href="05-data-visualization.html">Data visualization</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/data-lessons/R-genomics">
<span class="fa fa-github fa-lg"></span>
</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Introduction to R</h1>
<h4 class="author"><em>Data Carpentry contributors</em></h4>
</div>
<hr />
<blockquote>
<h2>Learning Objectives</h2>
<ul>
<li>Assign names to objects in R.</li>
<li>Solve mathematical operations in R.</li>
<li>Describe what a function is in R. Describe what vectors are and how they are manipulated in R.</li>
<li>Inspect the content of vectors in R and manipulate their content.</li>
<li>Employ logical subsetting (i.e. TRUE, FALSE) to extract data from a vector.</li>
</ul>
</blockquote>
<hr />
<div id="the-r-syntax" class="section level2">
<h2>The R syntax</h2>
<p><em>Start by showing an example of a script</em></p>
<ul>
<li>Point to the different parts:</li>
<li>a function</li>
<li>the assignment operator <code><-</code></li>
<li>the <code>=</code> for arguments</li>
<li>the comments <code>#</code> and how they are used to document function and its content</li>
<li>the <code>$</code> operator</li>
<li>Point to indentation and consistency in spacing to improve clarity</li>
</ul>
</div>
<div id="creating-objects" class="section level2">
<h2>Creating objects</h2>
<p>You can get output from R simply by typing in math in the console</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="dv">3</span> +<span class="st"> </span><span class="dv">5</span>
<span class="dv">12</span>/<span class="dv">7</span></code></pre>
<p>We can also comment what it is we’re doing</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="co"># I am adding 3 and 5. R is fun!</span>
<span class="dv">3+5</span></code></pre>
<p>What happens if we do that same command with the # sign in the front?</p>
<pre class="sourceCode r"><code class="sourceCode r">I am adding <span class="dv">3</span> and <span class="dv">5</span>. R is fun!
<span class="dv">3+5</span></code></pre>
<p>Now R is trying to run that sentence as a command, and it doesn’t work. Now we’re stuck over in the console. The <code>+</code> sign means that it’s still waiting for input, so we can’t type in a new command. To get out of this type <code>Esc</code>. This will work whenever you’re stuck with that <code>+</code> sign.</p>
<p>It’s great that R is a glorified caluculator, but obviously we want to do more interesting things.</p>
<p>To do useful and interesting things, we need to assign <em>values</em> to <em>objects</em>. To create objects, we need to give it a name followed by the assignment operator <code><-</code> and the value we want to give it.</p>
</div>
<div id="assignment-operator" class="section level2">
<h2>Assignment operator</h2>
<p>For instance, instead of adding 3 + 5, we can assign those values to objects and then add them.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="co"># assign 3 to a</span>
a <-<span class="st"> </span><span class="dv">3</span>
<span class="co"># assign 5 to b</span>
b <-<span class="st"> </span><span class="dv">5</span>
<span class="co"># what now is a</span>
a
<span class="co"># what now is b</span>
b
<span class="co">#Add a and b</span>
a +<span class="st"> </span>b</code></pre>
<p><code><-</code> is the assignment operator. It assigns values on the right to objects on the left. So, after executing <code>x <- 3</code>, the value of <code>x</code> is <code>3</code>. The arrow can be read as 3 <strong>goes into</strong> <code>x</code>. You can also use <code>=</code> for assignments but not in all contexts so it is good practice to use <code><-</code> for assignments. <code>=</code> should only be used to specify the values of arguments in functions, see below.</p>
<p>In RStudio, typing <code>Alt + -</code> (push <code>Alt</code>, the key next to your space bar at the same time as the <code>-</code> key) will write <code><-</code> in a single keystroke.</p>
<div id="exercise" class="section level3">
<h3>Exercise</h3>
<ul>
<li>What happens if we change <code>a</code> and then re-add <code>a</code> and <code>b</code>?</li>
<li>Does it work if you just change <code>a</code> in the script and then add <code>a</code> and <code>b</code>? Did you still get the same answer after they changed <code>a</code>? If so, why do you think that might be?</li>
<li>We can also assign a + b to a new variable, <code>c</code>. How would you do this?</li>
</ul>
</div>
</div>
<div id="notes-on-objects" class="section level2">
<h2>Notes on objects</h2>
<p>Objects can be given any name such as <code>x</code>, <code>current_temperature</code>, or <code>subject_id</code>. You want your object names to be explicit and not too long. They cannot start with a number (<code>2x</code> is not valid but <code>x2</code> is). R is case sensitive (e.g., <code>Genome_length_mb</code> is different from <code>genome_length_mb</code>). There are some names that cannot be used because they represent the names of fundamental functions in R (e.g., <code>if</code>, <code>else</code>, <code>for</code>, see <a href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/Reserved.html">here</a> for a complete list). In general, even if it’s allowed, it’s best to not use other function names (e.g., <code>c</code>, <code>T</code>, <code>mean</code>, <code>data</code>, <code>df</code>, <code>weights</code>). In doubt check the help to see if the name is already in use. It’s also best to avoid dots (<code>.</code>) within a variable name as in <code>my.dataset</code>. There are many functions in R with dots in their names for historical reasons, but because dots have a special meaning in R (for methods) and other programming languages, it’s best to avoid them. It is also recommended to use nouns for variable names, and verbs for function names. It’s important to be consistent in the styling of your code (where you put spaces, how you name variable, etc.). In R, two popular style guides are <a href="http://adv-r.had.co.nz/Style.html">Hadley Wickham’s</a> and <a href="https://google-styleguide.googlecode.com/svn/trunk/Rguide.xml">Google’s</a>.</p>
<p>When assigning a value to an object, R does not print anything. You can force to print the value by using parentheses or by typing the name:</p>
</div>
<div id="functions" class="section level2">
<h2>Functions</h2>
<p>The other key feature of R are functions. These are R’s built in capabilities. Some examples of these are mathematical functions, like <code>sqrt</code> and <code>round</code>. You can also get functions from libraries (which we’ll talk about in a bit), or even write your own.</p>
</div>
<div id="functions-and-their-arguments" class="section level2">
<h2>Functions and their arguments</h2>
<p>Functions are “canned scripts” that automate something complicated or convenient or both. Many functions are predefined, or become available when using the function <code>library()</code> (more on that later). A function usually gets one or more inputs called <em>arguments</em>. Functions often (but not always) return a <em>value</em>. A typical example would be the function <code>sqrt()</code>. The input (the argument) must be a number, and the return value (in fact, the output) is the square root of that number. Executing a function (‘running it’) is called <em>calling</em> the function. An example of a function call is:</p>
<p><code>sqrt(a)</code></p>
<p>Here, the value of <code>a</code> is given to the <code>sqrt()</code> function, the <code>sqrt()</code> function calculates the square root. This function is very simple, because it takes just one argument.</p>
<p>The return ‘value’ of a function need not be numerical (like that of <code>sqrt()</code>), and it also does not need to be a single item: it can be a set of things, or even a data set. We’ll see that when we read data files in to R.</p>
<p>Arguments can be anything, not only numbers or filenames, but also other objects. Exactly what each argument means differs per function, and must be looked up in the documentation (see below). If an argument alters the way the function operates, such as whether to ignore ‘bad values’, such an argument is sometimes called an <em>option</em>.</p>
<p>Most functions can take several arguments, but many have so-called <em>defaults</em>. If you don’t specify such an argument when calling the function, the function itself will fall back on using the <em>default</em>. This is a standard value that the author of the function specified as being “good enough in standard cases”. An example would be what symbol to use in a plot. However, if you want something specific, simply change the argument yourself with a value of your choice.</p>
<p>Let’s try a function that can take multiple arguments <code>round</code>.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">round</span>(<span class="fl">3.14159</span>)</code></pre>
<pre><code>## [1] 3</code></pre>
<p>We can see that we get <code>3</code>. That’s because the default is to round to the nearest whole number. If we want more digits we can see how to do that by getting information about the <code>round</code> function. We can use <code>args(round)</code> or look at the help for this function using <code>?round</code>.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">args</span>(round)</code></pre>
<pre><code>## function (x, digits = 0)
## NULL</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">?round</code></pre>
<p>We see that if we want a different number of digits, we can type <code>digits=2</code> or however many we want.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">round</span>(<span class="fl">3.14159</span>, <span class="dt">digits=</span><span class="dv">2</span>)</code></pre>
<pre><code>## [1] 3.14</code></pre>
<p>If you provide the arguments in the exact same order as they are defined you don’t have to name them:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">round</span>(<span class="fl">3.14159</span>, <span class="dv">2</span>)</code></pre>
<pre><code>## [1] 3.14</code></pre>
<p>However, it’s usually not recommended practice because it’s a lot of remembering to do, and if you share your code with others that includes less known functions it makes your code difficult to read. (It’s however OK to not include the names of the arguments for basic functions like <code>mean</code>, <code>min</code>, etc…)</p>
<p>Another advantage of naming arguments, is that the order doesn’t matter. This is useful when there start to be more arguments.</p>
<div id="exercise-1" class="section level3">
<h3>Exercise</h3>
<p>We’re going to work with genome lengths. - Create a variable <code>genome_length_mb</code> and assign it the value <code>4.6</code></p>
</div>
</div>
<div id="solution" class="section level2">
<h2>Solution</h2>
<pre class="sourceCode r"><code class="sourceCode r">(genome_length_mb <-<span class="st"> </span><span class="fl">4.6</span>)
genome_length_mb</code></pre>
<div id="exercise-2" class="section level3">
<h3>Exercise</h3>
<p>Now that R has <code>genome_length_mb</code> in memory, we can do arithmetic with it. For instance, we may want to convert this to the weight of the genome in picograms (for some reason). 978Mb = 1picogram. Divide the genome length in Mb by 978.</p>
</div>
<div id="solution-1" class="section level3">
<h3>Solution</h3>
<pre class="sourceCode r"><code class="sourceCode r">genome_length_mb /<span class="st"> </span><span class="fl">978.0</span></code></pre>
<p>It turns out an E. coli genome doesn’t weigh very much.</p>
</div>
<div id="exercise-3" class="section level3">
<h3>Exercise</h3>
<p>We can also change the variable’s value by assigning it a new one. Say we want to think about a human genome rather than E. coli. Change <code>genome_length_mb</code> to 3000 and figure out the weight of the human genome.</p>
</div>
<div id="solution-2" class="section level3">
<h3>Solution</h3>
<pre class="sourceCode r"><code class="sourceCode r">genome_length_mb <-<span class="st"> </span><span class="fl">3000.0</span>
genome_length_mb /<span class="st"> </span><span class="fl">978.0</span></code></pre>
<p>This means that assigning a value to one variable does not change the values of other variables. For example, let’s store the genome’s weight in a variable.</p>
<pre class="sourceCode r"><code class="sourceCode r">genome_weight_pg <-<span class="st"> </span>genome_length_mb /<span class="st"> </span><span class="fl">978.0</span></code></pre>
<p>and then change <code>genome_length_mb</code> to 100.</p>
<pre class="sourceCode r"><code class="sourceCode r">genome_length_mb <-<span class="st"> </span><span class="dv">100</span></code></pre>
</div>
<div id="exercise-4" class="section level3">
<h3>Exercise</h3>
<p>What do you think is the current content of the object <code>genome_weight_pg</code>? 3.06 or 0.102?</p>
</div>
</div>
<div id="vectors-and-data-types" class="section level2">
<h2>Vectors and data types</h2>
<p>A vector is the most common and basic data structure in R, and is pretty much the workhorse of R. It’s basically just a list of values, mainly either numbers or characters. They’re special lists that you can do math with. You can assign this list of values to a variable, just like you would for one item. For example we can create a vector of genome lengths:</p>
<pre class="sourceCode r"><code class="sourceCode r">glengths <-<span class="st"> </span><span class="kw">c</span>(<span class="fl">4.6</span>, <span class="dv">3000</span>, <span class="dv">50000</span>)
glengths</code></pre>
<p>A vector can also contain characters:</p>
<pre class="sourceCode r"><code class="sourceCode r">species <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"ecoli"</span>, <span class="st">"human"</span>, <span class="st">"corn"</span>)
species</code></pre>
<p>There are many functions that allow you to inspect the content of a vector. <code>length()</code> tells you how many elements are in a particular vector:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">length</span>(glengths)
<span class="kw">length</span>(species)</code></pre>
<p>You can also do math with whole vectors. For instance if we wanted to multiply the genome lengths of all the genomes in the list, we can do</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="dv">5</span> *<span class="st"> </span>glengths</code></pre>
<p>or we can add the data in the two vectors together</p>
<pre class="sourceCode r"><code class="sourceCode r">new_lengths <-<span class="st"> </span>glengths +<span class="st"> </span>glengths
new_lengths</code></pre>
<p>This is very useful if we have data in different vectors that we want to combine or work with.</p>
<p>There are few ways to figure out what’s going on in a vector.</p>
<p><code>class()</code> indicates the class (the type of element) of an object:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">class</span>(glengths)
<span class="kw">class</span>(species)</code></pre>
<p>The function <code>str()</code> provides an overview of the object and the elements it contains. It is a really useful function when working with large and complex objects:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">str</span>(glengths)
<span class="kw">str</span>(species)</code></pre>
<p>You can add elements to your vector simply by using the <code>c()</code> function:</p>
<pre class="sourceCode r"><code class="sourceCode r">lengths <-<span class="st"> </span><span class="kw">c</span>(glengths, <span class="dv">90</span>) <span class="co"># adding at the end</span>
lengths <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">30</span>, glengths) <span class="co"># adding at the beginning</span>
lengths</code></pre>
<p>What happens here is that we take the original vector <code>glengths</code>, and we are adding another item first to the end of the other ones, and then another item at the beginning. We can do this over and over again to build a vector or a dataset. As we program, this may be useful to autoupdate results that we are collecting or calculating.</p>
<p>We just saw 2 of the 6 <strong>data types</strong> that R uses: <code>"character"</code> and <code>"numeric"</code>. The other 4 are:</p>
<ul>
<li><code>"logical"</code> for <code>TRUE</code> and <code>FALSE</code> (the boolean data type)</li>
<li><code>"integer"</code> for integer numbers (e.g., <code>2L</code>, the <code>L</code> indicates to R that it’s an integer)</li>
<li><code>"complex"</code> to represent complex numbers with real and imaginary parts (e.g., <code>1+4i</code>) and that’s all we’re going to say about them</li>
<li><code>"raw"</code> that we won’t discuss further</li>
</ul>
<p>Vectors are one of the many <strong>data structures</strong> that R uses. Other important ones are lists (<code>list</code>), matrices (<code>matrix</code>), data frames (<code>data.frame</code>) and factors (<code>factor</code>).</p>
</div>
<hr/>
<p><a href="http://datacarpentry.org/">Data Carpentry</a>,
2017. <a href="LICENSE.html">License</a>. <a href="CONTRIBUTING.html">Contributing</a>. <br/>
Questions? Feedback?
Please <a href="https://github.com/datacarpentry/R-ecology-lesson/issues/new">file
an issue on GitHub</a>. <br/> On
Twitter: <a href="https://twitter.com/datacarpentry">@datacarpentry</a></p>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>