-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvectors.html
More file actions
929 lines (892 loc) · 97.1 KB
/
vectors.html
File metadata and controls
929 lines (892 loc) · 97.1 KB
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 7 Vectors | Technical Foundations of Informatics</title>
<meta name="description" content="The course reader for INFO 201: Technical Foundations of Informatics." />
<meta name="generator" content="bookdown 0.13 and GitBook 2.6.7" />
<meta property="og:title" content="Chapter 7 Vectors | Technical Foundations of Informatics" />
<meta property="og:type" content="book" />
<meta property="og:url" content="https://info201.github.io/" />
<meta property="og:image" content="https://info201.github.io/img/cover-img.png" />
<meta property="og:description" content="The course reader for INFO 201: Technical Foundations of Informatics." />
<meta name="github-repo" content="info201/book" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Chapter 7 Vectors | Technical Foundations of Informatics" />
<meta name="twitter:description" content="The course reader for INFO 201: Technical Foundations of Informatics." />
<meta name="twitter:image" content="https://info201.github.io/img/cover-img.png" />
<meta name="author" content="Michael Freeman and Joel Ross" />
<meta name="date" content="2019-09-11" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="shortcut icon" href="img/favicon.png" type="image/x-icon" />
<link rel="prev" href="functions.html"/>
<link rel="next" href="lists.html"/>
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-98444716-2', 'auto');
ga('send', 'pageview');
</script>
<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
{ position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
{ content: attr(data-line-number);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; pointer-events: all; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li><a href="./">Technical Foundations of Informatics</a></li>
<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>About the Book</a></li>
<li class="chapter" data-level="1" data-path="setup-machine.html"><a href="setup-machine.html"><i class="fa fa-check"></i><b>1</b> Setting up your Machine</a><ul>
<li class="chapter" data-level="1.1" data-path="setup-machine.html"><a href="setup-machine.html#git"><i class="fa fa-check"></i><b>1.1</b> Git</a><ul>
<li class="chapter" data-level="1.1.1" data-path="setup-machine.html"><a href="setup-machine.html#github"><i class="fa fa-check"></i><b>1.1.1</b> GitHub</a></li>
</ul></li>
<li class="chapter" data-level="1.2" data-path="setup-machine.html"><a href="setup-machine.html#command-line-tools-bash"><i class="fa fa-check"></i><b>1.2</b> Command-line Tools (Bash)</a><ul>
<li class="chapter" data-level="1.2.1" data-path="setup-machine.html"><a href="setup-machine.html#command-line-on-a-mac"><i class="fa fa-check"></i><b>1.2.1</b> Command-line on a Mac</a></li>
<li class="chapter" data-level="1.2.2" data-path="setup-machine.html"><a href="setup-machine.html#command-line-on-windows"><i class="fa fa-check"></i><b>1.2.2</b> Command-line on Windows</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="setup-machine.html"><a href="setup-machine.html#text-editors"><i class="fa fa-check"></i><b>1.3</b> Text Editors</a><ul>
<li class="chapter" data-level="1.3.1" data-path="setup-machine.html"><a href="setup-machine.html#atom"><i class="fa fa-check"></i><b>1.3.1</b> Atom</a></li>
<li class="chapter" data-level="1.3.2" data-path="setup-machine.html"><a href="setup-machine.html#visual-studio-code"><i class="fa fa-check"></i><b>1.3.2</b> Visual Studio Code</a></li>
<li class="chapter" data-level="1.3.3" data-path="setup-machine.html"><a href="setup-machine.html#sublime-text"><i class="fa fa-check"></i><b>1.3.3</b> Sublime Text</a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path="setup-machine.html"><a href="setup-machine.html#r-language"><i class="fa fa-check"></i><b>1.4</b> R Language</a></li>
<li class="chapter" data-level="1.5" data-path="setup-machine.html"><a href="setup-machine.html#rstudio"><i class="fa fa-check"></i><b>1.5</b> RStudio</a></li>
<li class="chapter" data-level="" data-path="setup-machine.html"><a href="setup-machine.html#resources"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="command-line.html"><a href="command-line.html"><i class="fa fa-check"></i><b>2</b> The Command Line</a><ul>
<li class="chapter" data-level="2.1" data-path="command-line.html"><a href="command-line.html#accessing-the-command-line"><i class="fa fa-check"></i><b>2.1</b> Accessing the Command-Line</a></li>
<li class="chapter" data-level="2.2" data-path="command-line.html"><a href="command-line.html#navigating-the-command-line"><i class="fa fa-check"></i><b>2.2</b> Navigating the Command Line</a><ul>
<li class="chapter" data-level="2.2.1" data-path="command-line.html"><a href="command-line.html#changing-directories"><i class="fa fa-check"></i><b>2.2.1</b> Changing Directories</a></li>
<li class="chapter" data-level="2.2.2" data-path="command-line.html"><a href="command-line.html#listing-files"><i class="fa fa-check"></i><b>2.2.2</b> Listing Files</a></li>
<li class="chapter" data-level="2.2.3" data-path="command-line.html"><a href="command-line.html#paths"><i class="fa fa-check"></i><b>2.2.3</b> Paths</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="command-line.html"><a href="command-line.html#file-commands"><i class="fa fa-check"></i><b>2.3</b> File Commands</a><ul>
<li class="chapter" data-level="2.3.1" data-path="command-line.html"><a href="command-line.html#learning-new-commands"><i class="fa fa-check"></i><b>2.3.1</b> Learning New Commands</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="command-line.html"><a href="command-line.html#dealing-with-errors"><i class="fa fa-check"></i><b>2.4</b> Dealing With Errors</a></li>
<li class="chapter" data-level="" data-path="command-line.html"><a href="command-line.html#resources-1"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="markdown.html"><a href="markdown.html"><i class="fa fa-check"></i><b>3</b> Markdown</a><ul>
<li class="chapter" data-level="3.1" data-path="markdown.html"><a href="markdown.html#writing-markdown"><i class="fa fa-check"></i><b>3.1</b> Writing Markdown</a><ul>
<li class="chapter" data-level="3.1.1" data-path="markdown.html"><a href="markdown.html#text-formatting"><i class="fa fa-check"></i><b>3.1.1</b> Text Formatting</a></li>
<li class="chapter" data-level="3.1.2" data-path="markdown.html"><a href="markdown.html#text-blocks"><i class="fa fa-check"></i><b>3.1.2</b> Text Blocks</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="markdown.html"><a href="markdown.html#rendering-markdown"><i class="fa fa-check"></i><b>3.2</b> Rendering Markdown</a></li>
<li class="chapter" data-level="" data-path="markdown.html"><a href="markdown.html#resources-2"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="git-basics.html"><a href="git-basics.html"><i class="fa fa-check"></i><b>4</b> Git and GitHub</a><ul>
<li class="chapter" data-level="4.1" data-path="git-basics.html"><a href="git-basics.html#what-is-this-git-thing-anyway"><i class="fa fa-check"></i><b>4.1</b> What is this <em>git</em> thing anyway?</a><ul>
<li class="chapter" data-level="4.1.1" data-path="git-basics.html"><a href="git-basics.html#git-core-concepts"><i class="fa fa-check"></i><b>4.1.1</b> Git Core Concepts</a></li>
<li class="chapter" data-level="4.1.2" data-path="git-basics.html"><a href="git-basics.html#wait-but-what-is-github-then"><i class="fa fa-check"></i><b>4.1.2</b> Wait, but what is GitHub then?</a></li>
</ul></li>
<li class="chapter" data-level="4.2" data-path="git-basics.html"><a href="git-basics.html#installation-setup"><i class="fa fa-check"></i><b>4.2</b> Installation & Setup</a><ul>
<li class="chapter" data-level="4.2.1" data-path="git-basics.html"><a href="git-basics.html#creating-a-repo"><i class="fa fa-check"></i><b>4.2.1</b> Creating a Repo</a></li>
<li class="chapter" data-level="4.2.2" data-path="git-basics.html"><a href="git-basics.html#checking-status"><i class="fa fa-check"></i><b>4.2.2</b> Checking Status</a></li>
</ul></li>
<li class="chapter" data-level="4.3" data-path="git-basics.html"><a href="git-basics.html#making-changes"><i class="fa fa-check"></i><b>4.3</b> Making Changes</a><ul>
<li class="chapter" data-level="4.3.1" data-path="git-basics.html"><a href="git-basics.html#adding-files"><i class="fa fa-check"></i><b>4.3.1</b> Adding Files</a></li>
<li class="chapter" data-level="4.3.2" data-path="git-basics.html"><a href="git-basics.html#committing"><i class="fa fa-check"></i><b>4.3.2</b> Committing</a></li>
<li class="chapter" data-level="4.3.3" data-path="git-basics.html"><a href="git-basics.html#commit-history"><i class="fa fa-check"></i><b>4.3.3</b> Commit History</a></li>
<li class="chapter" data-level="4.3.4" data-path="git-basics.html"><a href="git-basics.html#reviewing-the-process"><i class="fa fa-check"></i><b>4.3.4</b> Reviewing the Process</a></li>
<li class="chapter" data-level="4.3.5" data-path="git-basics.html"><a href="git-basics.html#gitignore"><i class="fa fa-check"></i><b>4.3.5</b> The <code>.gitignore</code> File</a></li>
</ul></li>
<li class="chapter" data-level="4.4" data-path="git-basics.html"><a href="git-basics.html#github-and-remotes"><i class="fa fa-check"></i><b>4.4</b> GitHub and Remotes</a><ul>
<li class="chapter" data-level="4.4.1" data-path="git-basics.html"><a href="git-basics.html#forking-and-cloning"><i class="fa fa-check"></i><b>4.4.1</b> Forking and Cloning</a></li>
<li class="chapter" data-level="4.4.2" data-path="git-basics.html"><a href="git-basics.html#pushing-and-pulling"><i class="fa fa-check"></i><b>4.4.2</b> Pushing and Pulling</a></li>
<li class="chapter" data-level="4.4.3" data-path="git-basics.html"><a href="git-basics.html#reviewing-the-process-1"><i class="fa fa-check"></i><b>4.4.3</b> Reviewing The Process</a></li>
</ul></li>
<li class="chapter" data-level="4.5" data-path="git-basics.html"><a href="git-basics.html#course-assignments-on-github"><i class="fa fa-check"></i><b>4.5</b> Course Assignments on GitHub</a></li>
<li class="chapter" data-level="4.6" data-path="git-basics.html"><a href="git-basics.html#command-summary"><i class="fa fa-check"></i><b>4.6</b> Command Summary</a></li>
<li class="chapter" data-level="" data-path="git-basics.html"><a href="git-basics.html#resources-3"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="r-intro.html"><a href="r-intro.html"><i class="fa fa-check"></i><b>5</b> Introduction to R</a><ul>
<li class="chapter" data-level="5.1" data-path="r-intro.html"><a href="r-intro.html#programming-with-r"><i class="fa fa-check"></i><b>5.1</b> Programming with R</a></li>
<li class="chapter" data-level="5.2" data-path="r-intro.html"><a href="r-intro.html#running-r-scripts"><i class="fa fa-check"></i><b>5.2</b> Running R Scripts</a><ul>
<li class="chapter" data-level="5.2.1" data-path="r-intro.html"><a href="r-intro.html#running-r-cmd"><i class="fa fa-check"></i><b>5.2.1</b> Command-Line</a></li>
<li class="chapter" data-level="5.2.2" data-path="r-intro.html"><a href="r-intro.html#running-r-rstudio"><i class="fa fa-check"></i><b>5.2.2</b> RStudio</a></li>
</ul></li>
<li class="chapter" data-level="5.3" data-path="r-intro.html"><a href="r-intro.html#comments"><i class="fa fa-check"></i><b>5.3</b> Comments</a></li>
<li class="chapter" data-level="5.4" data-path="r-intro.html"><a href="r-intro.html#variables"><i class="fa fa-check"></i><b>5.4</b> Variables</a><ul>
<li class="chapter" data-level="5.4.1" data-path="r-intro.html"><a href="r-intro.html#basic-data-types"><i class="fa fa-check"></i><b>5.4.1</b> Basic Data Types</a></li>
</ul></li>
<li class="chapter" data-level="5.5" data-path="r-intro.html"><a href="r-intro.html#gettinghelp"><i class="fa fa-check"></i><b>5.5</b> Getting Help</a></li>
<li class="chapter" data-level="" data-path="r-intro.html"><a href="r-intro.html#resources-4"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="functions.html"><a href="functions.html"><i class="fa fa-check"></i><b>6</b> Functions</a><ul>
<li class="chapter" data-level="6.1" data-path="functions.html"><a href="functions.html#what-are-functions"><i class="fa fa-check"></i><b>6.1</b> What are Functions?</a></li>
<li class="chapter" data-level="6.2" data-path="functions.html"><a href="functions.html#how-to-use-functions"><i class="fa fa-check"></i><b>6.2</b> How to Use Functions</a></li>
<li class="chapter" data-level="6.3" data-path="functions.html"><a href="functions.html#built-in-r-functions"><i class="fa fa-check"></i><b>6.3</b> Built-in R Functions</a></li>
<li class="chapter" data-level="6.4" data-path="functions.html"><a href="functions.html#loading-functions"><i class="fa fa-check"></i><b>6.4</b> Loading Functions</a></li>
<li class="chapter" data-level="6.5" data-path="functions.html"><a href="functions.html#writing-functions"><i class="fa fa-check"></i><b>6.5</b> Writing Functions</a></li>
<li class="chapter" data-level="6.6" data-path="functions.html"><a href="functions.html#conditional-statements"><i class="fa fa-check"></i><b>6.6</b> Conditional Statements</a></li>
<li class="chapter" data-level="" data-path="functions.html"><a href="functions.html#resources-5"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="vectors.html"><a href="vectors.html"><i class="fa fa-check"></i><b>7</b> Vectors</a><ul>
<li class="chapter" data-level="7.1" data-path="vectors.html"><a href="vectors.html#what-is-a-vector"><i class="fa fa-check"></i><b>7.1</b> What is a Vector?</a></li>
<li class="chapter" data-level="7.2" data-path="vectors.html"><a href="vectors.html#creating-vectors"><i class="fa fa-check"></i><b>7.2</b> Creating Vectors</a></li>
<li class="chapter" data-level="7.3" data-path="vectors.html"><a href="vectors.html#vector-indices"><i class="fa fa-check"></i><b>7.3</b> Vector Indices</a><ul>
<li class="chapter" data-level="7.3.1" data-path="vectors.html"><a href="vectors.html#simple-numeric-indices"><i class="fa fa-check"></i><b>7.3.1</b> Simple Numeric Indices</a></li>
<li class="chapter" data-level="7.3.2" data-path="vectors.html"><a href="vectors.html#multiple-indices"><i class="fa fa-check"></i><b>7.3.2</b> Multiple Indices</a></li>
<li class="chapter" data-level="7.3.3" data-path="vectors.html"><a href="vectors.html#logical-indexing"><i class="fa fa-check"></i><b>7.3.3</b> Logical Indexing</a></li>
<li class="chapter" data-level="7.3.4" data-path="vectors.html"><a href="vectors.html#named-vectors-and-character-indexing"><i class="fa fa-check"></i><b>7.3.4</b> Named Vectors and Character Indexing</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="vectors.html"><a href="vectors.html#modifying-vectors"><i class="fa fa-check"></i><b>7.4</b> Modifying Vectors</a></li>
<li class="chapter" data-level="7.5" data-path="vectors.html"><a href="vectors.html#vectorized-operations"><i class="fa fa-check"></i><b>7.5</b> Vectorized Operations</a><ul>
<li class="chapter" data-level="7.5.1" data-path="vectors.html"><a href="vectors.html#vectorized-operators"><i class="fa fa-check"></i><b>7.5.1</b> Vectorized Operators</a></li>
<li class="chapter" data-level="7.5.2" data-path="vectors.html"><a href="vectors.html#vectorized-functions"><i class="fa fa-check"></i><b>7.5.2</b> Vectorized Functions</a></li>
<li class="chapter" data-level="7.5.3" data-path="vectors.html"><a href="vectors.html#recycling"><i class="fa fa-check"></i><b>7.5.3</b> Recycling</a></li>
<li class="chapter" data-level="7.5.4" data-path="vectors.html"><a href="vectors.html#r-is-a-vectorized-world"><i class="fa fa-check"></i><b>7.5.4</b> R Is a Vectorized World</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="vectors.html"><a href="vectors.html#resources-6"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="lists.html"><a href="lists.html"><i class="fa fa-check"></i><b>8</b> Lists</a><ul>
<li class="chapter" data-level="8.1" data-path="lists.html"><a href="lists.html#what-is-a-list"><i class="fa fa-check"></i><b>8.1</b> What is a List?</a></li>
<li class="chapter" data-level="8.2" data-path="lists.html"><a href="lists.html#creating-lists"><i class="fa fa-check"></i><b>8.2</b> Creating Lists</a></li>
<li class="chapter" data-level="8.3" data-path="lists.html"><a href="lists.html#accessing-list-elements"><i class="fa fa-check"></i><b>8.3</b> Accessing List Elements</a><ul>
<li class="chapter" data-level="8.3.1" data-path="lists.html"><a href="lists.html#lists-indexing-by-position"><i class="fa fa-check"></i><b>8.3.1</b> Indexing by position</a></li>
<li class="chapter" data-level="8.3.2" data-path="lists.html"><a href="lists.html#indexing-by-name"><i class="fa fa-check"></i><b>8.3.2</b> Indexing by Name</a></li>
<li class="chapter" data-level="8.3.3" data-path="lists.html"><a href="lists.html#indexing-by-logical-vector"><i class="fa fa-check"></i><b>8.3.3</b> Indexing by Logical Vector</a></li>
<li class="chapter" data-level="8.3.4" data-path="lists.html"><a href="lists.html#lists-dollar-shortcut"><i class="fa fa-check"></i><b>8.3.4</b> Extracting named elements with <code>$</code></a></li>
<li class="chapter" data-level="8.3.5" data-path="lists.html"><a href="lists.html#single-vs.double-brackets-vs.dollar"><i class="fa fa-check"></i><b>8.3.5</b> Single vs. Double Brackets vs. Dollar</a></li>
</ul></li>
<li class="chapter" data-level="8.4" data-path="lists.html"><a href="lists.html#modifying-lists"><i class="fa fa-check"></i><b>8.4</b> Modifying Lists</a></li>
<li class="chapter" data-level="8.5" data-path="lists.html"><a href="lists.html#the-lapply-function"><i class="fa fa-check"></i><b>8.5</b> The <code>lapply()</code> Function</a></li>
<li class="chapter" data-level="" data-path="lists.html"><a href="lists.html#resources-7"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="data-frames.html"><a href="data-frames.html"><i class="fa fa-check"></i><b>9</b> Data Frames</a><ul>
<li class="chapter" data-level="9.1" data-path="data-frames.html"><a href="data-frames.html#what-is-a-data-frame"><i class="fa fa-check"></i><b>9.1</b> What is a Data Frame?</a><ul>
<li class="chapter" data-level="9.1.1" data-path="data-frames.html"><a href="data-frames.html#creating-data-frames"><i class="fa fa-check"></i><b>9.1.1</b> Creating Data Frames</a></li>
<li class="chapter" data-level="9.1.2" data-path="data-frames.html"><a href="data-frames.html#describing-structure-of-data-frames"><i class="fa fa-check"></i><b>9.1.2</b> Describing Structure of Data Frames</a></li>
<li class="chapter" data-level="9.1.3" data-path="data-frames.html"><a href="data-frames.html#accessing-data-in-data-frames"><i class="fa fa-check"></i><b>9.1.3</b> Accessing Data in Data Frames</a></li>
</ul></li>
<li class="chapter" data-level="9.2" data-path="data-frames.html"><a href="data-frames.html#csv-files"><i class="fa fa-check"></i><b>9.2</b> Working with CSV Data</a><ul>
<li class="chapter" data-level="9.2.1" data-path="data-frames.html"><a href="data-frames.html#working-directory"><i class="fa fa-check"></i><b>9.2.1</b> Working Directory</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="data-frames.html"><a href="data-frames.html#factors"><i class="fa fa-check"></i><b>9.3</b> Factor Variables</a></li>
<li class="chapter" data-level="" data-path="data-frames.html"><a href="data-frames.html#resources-8"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="dplyr.html"><a href="dplyr.html"><i class="fa fa-check"></i><b>10</b> The <code>dplyr</code> Library</a><ul>
<li class="chapter" data-level="10.1" data-path="dplyr.html"><a href="dplyr.html#a-grammar-of-data-manipulation"><i class="fa fa-check"></i><b>10.1</b> A Grammar of Data Manipulation</a></li>
<li class="chapter" data-level="10.2" data-path="dplyr.html"><a href="dplyr.html#using-dplyr-functions"><i class="fa fa-check"></i><b>10.2</b> Using <code>dplyr</code> Functions</a><ul>
<li class="chapter" data-level="10.2.1" data-path="dplyr.html"><a href="dplyr.html#select"><i class="fa fa-check"></i><b>10.2.1</b> Select</a></li>
<li class="chapter" data-level="10.2.2" data-path="dplyr.html"><a href="dplyr.html#filter"><i class="fa fa-check"></i><b>10.2.2</b> Filter</a></li>
<li class="chapter" data-level="10.2.3" data-path="dplyr.html"><a href="dplyr.html#mutate"><i class="fa fa-check"></i><b>10.2.3</b> Mutate</a></li>
<li class="chapter" data-level="10.2.4" data-path="dplyr.html"><a href="dplyr.html#arrange"><i class="fa fa-check"></i><b>10.2.4</b> Arrange</a></li>
<li class="chapter" data-level="10.2.5" data-path="dplyr.html"><a href="dplyr.html#summarize"><i class="fa fa-check"></i><b>10.2.5</b> Summarize</a></li>
<li class="chapter" data-level="10.2.6" data-path="dplyr.html"><a href="dplyr.html#distinct"><i class="fa fa-check"></i><b>10.2.6</b> Distinct</a></li>
</ul></li>
<li class="chapter" data-level="10.3" data-path="dplyr.html"><a href="dplyr.html#multiple-operations"><i class="fa fa-check"></i><b>10.3</b> Multiple Operations</a><ul>
<li class="chapter" data-level="10.3.1" data-path="dplyr.html"><a href="dplyr.html#the-pipe-operator"><i class="fa fa-check"></i><b>10.3.1</b> The Pipe Operator</a></li>
</ul></li>
<li class="chapter" data-level="10.4" data-path="dplyr.html"><a href="dplyr.html#grouped-operations"><i class="fa fa-check"></i><b>10.4</b> Grouped Operations</a></li>
<li class="chapter" data-level="10.5" data-path="dplyr.html"><a href="dplyr.html#joins"><i class="fa fa-check"></i><b>10.5</b> Joins</a></li>
<li class="chapter" data-level="10.6" data-path="dplyr.html"><a href="dplyr.html#non-standard-evaluation-vs.standard-evaluation"><i class="fa fa-check"></i><b>10.6</b> Non-Standard Evaluation vs. Standard Evaluation</a><ul>
<li class="chapter" data-level="10.6.1" data-path="dplyr.html"><a href="dplyr.html#explicit-standard-evaluation"><i class="fa fa-check"></i><b>10.6.1</b> Explicit Standard Evaluation</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="dplyr.html"><a href="dplyr.html#resources-9"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="apis.html"><a href="apis.html"><i class="fa fa-check"></i><b>11</b> Accessing Web APIs</a><ul>
<li class="chapter" data-level="11.1" data-path="apis.html"><a href="apis.html#what-is-a-web-api"><i class="fa fa-check"></i><b>11.1</b> What is a Web API?</a></li>
<li class="chapter" data-level="11.2" data-path="apis.html"><a href="apis.html#restful-requests"><i class="fa fa-check"></i><b>11.2</b> RESTful Requests</a><ul>
<li class="chapter" data-level="11.2.1" data-path="apis.html"><a href="apis.html#uris"><i class="fa fa-check"></i><b>11.2.1</b> URIs</a></li>
<li class="chapter" data-level="11.2.2" data-path="apis.html"><a href="apis.html#http-verbs"><i class="fa fa-check"></i><b>11.2.2</b> HTTP Verbs</a></li>
</ul></li>
<li class="chapter" data-level="11.3" data-path="apis.html"><a href="apis.html#accessing-web-apis"><i class="fa fa-check"></i><b>11.3</b> Accessing Web APIs</a></li>
<li class="chapter" data-level="11.4" data-path="apis.html"><a href="apis.html#json"><i class="fa fa-check"></i><b>11.4</b> JSON Data</a><ul>
<li class="chapter" data-level="11.4.1" data-path="apis.html"><a href="apis.html#parsing-json"><i class="fa fa-check"></i><b>11.4.1</b> Parsing JSON</a></li>
<li class="chapter" data-level="11.4.2" data-path="apis.html"><a href="apis.html#flattening-data"><i class="fa fa-check"></i><b>11.4.2</b> Flattening Data</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="apis.html"><a href="apis.html#resources-10"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="r-markdown.html"><a href="r-markdown.html"><i class="fa fa-check"></i><b>12</b> R Markdown</a><ul>
<li class="chapter" data-level="12.1" data-path="r-markdown.html"><a href="r-markdown.html#r-markdown-and-rstudio"><i class="fa fa-check"></i><b>12.1</b> R Markdown and RStudio</a><ul>
<li class="chapter" data-level="12.1.1" data-path="r-markdown.html"><a href="r-markdown.html#creating-.rmd-files"><i class="fa fa-check"></i><b>12.1.1</b> Creating <code>.Rmd</code> Files</a></li>
<li class="chapter" data-level="12.1.2" data-path="r-markdown.html"><a href="r-markdown.html#rmd-content"><i class="fa fa-check"></i><b>12.1.2</b> <code>.Rmd</code> Content</a></li>
<li class="chapter" data-level="12.1.3" data-path="r-markdown.html"><a href="r-markdown.html#knitting-documents"><i class="fa fa-check"></i><b>12.1.3</b> Knitting Documents</a></li>
<li class="chapter" data-level="12.1.4" data-path="r-markdown.html"><a href="r-markdown.html#html"><i class="fa fa-check"></i><b>12.1.4</b> HTML</a></li>
</ul></li>
<li class="chapter" data-level="12.2" data-path="r-markdown.html"><a href="r-markdown.html#r-markdown-syntax"><i class="fa fa-check"></i><b>12.2</b> R Markdown Syntax</a><ul>
<li class="chapter" data-level="12.2.1" data-path="r-markdown.html"><a href="r-markdown.html#r-code-chunks"><i class="fa fa-check"></i><b>12.2.1</b> R Code Chunks</a></li>
<li class="chapter" data-level="12.2.2" data-path="r-markdown.html"><a href="r-markdown.html#inline-code"><i class="fa fa-check"></i><b>12.2.2</b> Inline Code</a></li>
</ul></li>
<li class="chapter" data-level="12.3" data-path="r-markdown.html"><a href="r-markdown.html#rendering-data"><i class="fa fa-check"></i><b>12.3</b> Rendering Data</a><ul>
<li class="chapter" data-level="12.3.1" data-path="r-markdown.html"><a href="r-markdown.html#rendering-strings"><i class="fa fa-check"></i><b>12.3.1</b> Rendering Strings</a></li>
<li class="chapter" data-level="12.3.2" data-path="r-markdown.html"><a href="r-markdown.html#rendering-lists"><i class="fa fa-check"></i><b>12.3.2</b> Rendering Lists</a></li>
<li class="chapter" data-level="12.3.3" data-path="r-markdown.html"><a href="r-markdown.html#rendering-tables"><i class="fa fa-check"></i><b>12.3.3</b> Rendering Tables</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="r-markdown.html"><a href="r-markdown.html#resources-11"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="13" data-path="ggplot2.html"><a href="ggplot2.html"><i class="fa fa-check"></i><b>13</b> The <code>gglot2</code> Library</a><ul>
<li class="chapter" data-level="13.1" data-path="ggplot2.html"><a href="ggplot2.html#a-grammar-of-graphics"><i class="fa fa-check"></i><b>13.1</b> A Grammar of Graphics</a></li>
<li class="chapter" data-level="13.2" data-path="ggplot2.html"><a href="ggplot2.html#basic-plotting-with-ggplot2"><i class="fa fa-check"></i><b>13.2</b> Basic Plotting with <code>ggplot2</code></a><ul>
<li class="chapter" data-level="13.2.1" data-path="ggplot2.html"><a href="ggplot2.html#ggplot2-library"><i class="fa fa-check"></i><b>13.2.1</b> <em>ggplot2</em> library</a></li>
<li class="chapter" data-level="13.2.2" data-path="ggplot2.html"><a href="ggplot2.html#mpg-data"><i class="fa fa-check"></i><b>13.2.2</b> <em>mpg</em> data</a></li>
<li class="chapter" data-level="13.2.3" data-path="ggplot2.html"><a href="ggplot2.html#our-first-ggplot"><i class="fa fa-check"></i><b>13.2.3</b> Our first ggplot</a></li>
<li class="chapter" data-level="13.2.4" data-path="ggplot2.html"><a href="ggplot2.html#aesthetic-mappings"><i class="fa fa-check"></i><b>13.2.4</b> Aesthetic Mappings</a></li>
</ul></li>
<li class="chapter" data-level="13.3" data-path="ggplot2.html"><a href="ggplot2.html#complex-plots"><i class="fa fa-check"></i><b>13.3</b> Complex Plots</a><ul>
<li class="chapter" data-level="13.3.1" data-path="ggplot2.html"><a href="ggplot2.html#specifying-geometry"><i class="fa fa-check"></i><b>13.3.1</b> Specifying Geometry</a></li>
<li class="chapter" data-level="13.3.2" data-path="ggplot2.html"><a href="ggplot2.html#styling-with-scales"><i class="fa fa-check"></i><b>13.3.2</b> Styling with Scales</a></li>
<li class="chapter" data-level="13.3.3" data-path="ggplot2.html"><a href="ggplot2.html#coordinate-systems"><i class="fa fa-check"></i><b>13.3.3</b> Coordinate Systems</a></li>
<li class="chapter" data-level="13.3.4" data-path="ggplot2.html"><a href="ggplot2.html#facets"><i class="fa fa-check"></i><b>13.3.4</b> Facets</a></li>
<li class="chapter" data-level="13.3.5" data-path="ggplot2.html"><a href="ggplot2.html#labels-annotations"><i class="fa fa-check"></i><b>13.3.5</b> Labels & Annotations</a></li>
</ul></li>
<li class="chapter" data-level="13.4" data-path="ggplot2.html"><a href="ggplot2.html#plotting-in-scripts"><i class="fa fa-check"></i><b>13.4</b> Plotting in Scripts</a></li>
<li class="chapter" data-level="13.5" data-path="ggplot2.html"><a href="ggplot2.html#other-visualization-libraries"><i class="fa fa-check"></i><b>13.5</b> Other Visualization Libraries</a></li>
<li class="chapter" data-level="" data-path="ggplot2.html"><a href="ggplot2.html#resources-12"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="14" data-path="git-branches.html"><a href="git-branches.html"><i class="fa fa-check"></i><b>14</b> Git Branches</a><ul>
<li class="chapter" data-level="14.1" data-path="git-branches.html"><a href="git-branches.html#git-branches-1"><i class="fa fa-check"></i><b>14.1</b> Git Branches</a></li>
<li class="chapter" data-level="14.2" data-path="git-branches.html"><a href="git-branches.html#merging"><i class="fa fa-check"></i><b>14.2</b> Merging</a><ul>
<li class="chapter" data-level="14.2.1" data-path="git-branches.html"><a href="git-branches.html#merge-conflicts"><i class="fa fa-check"></i><b>14.2.1</b> Merge Conflicts</a></li>
</ul></li>
<li class="chapter" data-level="14.3" data-path="git-branches.html"><a href="git-branches.html#undoing-changes"><i class="fa fa-check"></i><b>14.3</b> Undoing Changes</a></li>
<li class="chapter" data-level="14.4" data-path="git-branches.html"><a href="git-branches.html#github-and-branches"><i class="fa fa-check"></i><b>14.4</b> GitHub and Branches</a><ul>
<li class="chapter" data-level="14.4.1" data-path="git-branches.html"><a href="git-branches.html#github-pages"><i class="fa fa-check"></i><b>14.4.1</b> GitHub Pages</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="git-branches.html"><a href="git-branches.html#resources-13"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="15" data-path="git-collaboration.html"><a href="git-collaboration.html"><i class="fa fa-check"></i><b>15</b> Git Collaboration</a><ul>
<li class="chapter" data-level="15.1" data-path="git-collaboration.html"><a href="git-collaboration.html#centralized-workflow"><i class="fa fa-check"></i><b>15.1</b> Centralized Workflow</a></li>
<li class="chapter" data-level="15.2" data-path="git-collaboration.html"><a href="git-collaboration.html#feature-branch-workflow"><i class="fa fa-check"></i><b>15.2</b> Feature Branch Workflow</a></li>
<li class="chapter" data-level="15.3" data-path="git-collaboration.html"><a href="git-collaboration.html#forking-workflow"><i class="fa fa-check"></i><b>15.3</b> Forking Workflow</a><ul>
<li class="chapter" data-level="15.3.1" data-path="git-collaboration.html"><a href="git-collaboration.html#pull-requests"><i class="fa fa-check"></i><b>15.3.1</b> Pull Requests</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="git-collaboration.html"><a href="git-collaboration.html#resources-14"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="16" data-path="shiny.html"><a href="shiny.html"><i class="fa fa-check"></i><b>16</b> The <code>shiny</code> Framework</a><ul>
<li class="chapter" data-level="16.1" data-path="shiny.html"><a href="shiny.html#creating-shiny-apps"><i class="fa fa-check"></i><b>16.1</b> Creating Shiny Apps</a><ul>
<li class="chapter" data-level="16.1.1" data-path="shiny.html"><a href="shiny.html#application-structure"><i class="fa fa-check"></i><b>16.1.1</b> Application Structure</a></li>
<li class="chapter" data-level="16.1.2" data-path="shiny.html"><a href="shiny.html#the-ui"><i class="fa fa-check"></i><b>16.1.2</b> The UI</a></li>
<li class="chapter" data-level="16.1.3" data-path="shiny.html"><a href="shiny.html#the-server"><i class="fa fa-check"></i><b>16.1.3</b> The Server</a></li>
</ul></li>
<li class="chapter" data-level="16.2" data-path="shiny.html"><a href="shiny.html#publishing-shiny-apps"><i class="fa fa-check"></i><b>16.2</b> Publishing Shiny Apps</a></li>
<li class="chapter" data-level="" data-path="shiny.html"><a href="shiny.html#resources-15"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="appendix"><span><b>Special Topics</b></span></li>
<li class="chapter" data-level="A" data-path="plotly.html"><a href="plotly.html"><i class="fa fa-check"></i><b>A</b> Plotly</a><ul>
<li class="chapter" data-level="A.1" data-path="plotly.html"><a href="plotly.html#getting-started"><i class="fa fa-check"></i><b>A.1</b> Getting Started</a></li>
<li class="chapter" data-level="A.2" data-path="plotly.html"><a href="plotly.html#basic-charts"><i class="fa fa-check"></i><b>A.2</b> Basic Charts</a></li>
<li class="chapter" data-level="A.3" data-path="plotly.html"><a href="plotly.html#layout"><i class="fa fa-check"></i><b>A.3</b> Layout</a></li>
<li class="chapter" data-level="A.4" data-path="plotly.html"><a href="plotly.html#hovers"><i class="fa fa-check"></i><b>A.4</b> Hovers</a></li>
<li class="chapter" data-level="" data-path="plotly.html"><a href="plotly.html#resources-16"><i class="fa fa-check"></i>Resources</a></li>
</ul></li>
<li class="chapter" data-level="B" data-path="control-structures.html"><a href="control-structures.html"><i class="fa fa-check"></i><b>B</b> R Language Control Structures</a><ul>
<li class="chapter" data-level="B.1" data-path="control-structures.html"><a href="control-structures.html#loops"><i class="fa fa-check"></i><b>B.1</b> Loops</a><ul>
<li class="chapter" data-level="B.1.1" data-path="control-structures.html"><a href="control-structures.html#for-loop"><i class="fa fa-check"></i><b>B.1.1</b> For Loop</a></li>
<li class="chapter" data-level="B.1.2" data-path="control-structures.html"><a href="control-structures.html#while-loop"><i class="fa fa-check"></i><b>B.1.2</b> While-loop</a></li>
<li class="chapter" data-level="B.1.3" data-path="control-structures.html"><a href="control-structures.html#repeat-loop"><i class="fa fa-check"></i><b>B.1.3</b> repeat-loop</a></li>
<li class="chapter" data-level="B.1.4" data-path="control-structures.html"><a href="control-structures.html#leaving-early-break-and-next"><i class="fa fa-check"></i><b>B.1.4</b> Leaving Early: <code>break</code> and <code>next</code></a></li>
<li class="chapter" data-level="B.1.5" data-path="control-structures.html"><a href="control-structures.html#when-not-to-use-loops-in-r"><i class="fa fa-check"></i><b>B.1.5</b> When (Not) To Use Loops In R?</a></li>
</ul></li>
<li class="chapter" data-level="B.2" data-path="control-structures.html"><a href="control-structures.html#more-about-if-and-else"><i class="fa fa-check"></i><b>B.2</b> More about <code>if</code> and <code>else</code></a><ul>
<li class="chapter" data-level="B.2.1" data-path="control-structures.html"><a href="control-structures.html#where-to-put-else"><i class="fa fa-check"></i><b>B.2.1</b> Where To Put Else</a></li>
<li class="chapter" data-level="B.2.2" data-path="control-structures.html"><a href="control-structures.html#return-value"><i class="fa fa-check"></i><b>B.2.2</b> Return Value</a></li>
</ul></li>
<li class="chapter" data-level="B.3" data-path="control-structures.html"><a href="control-structures.html#switch-choosing-between-multiple-conditions"><i class="fa fa-check"></i><b>B.3</b> <code>switch</code>: Choosing Between Multiple Conditions</a></li>
</ul></li>
<li class="chapter" data-level="C" data-path="data-tables.html"><a href="data-tables.html"><i class="fa fa-check"></i><b>C</b> Thinking Big: Data Tables</a><ul>
<li class="chapter" data-level="C.1" data-path="data-tables.html"><a href="data-tables.html#background-passing-by-value-and-passing-by-reference"><i class="fa fa-check"></i><b>C.1</b> Background: Passing By Value And Passing By Reference</a></li>
<li class="chapter" data-level="C.2" data-path="data-tables.html"><a href="data-tables.html#data-tables-introduction"><i class="fa fa-check"></i><b>C.2</b> Data Tables: Introduction</a><ul>
<li class="chapter" data-level="C.2.1" data-path="data-tables.html"><a href="data-tables.html#replacement-for-data-frames-sort-of"><i class="fa fa-check"></i><b>C.2.1</b> Replacement for Data Frames (Sort of)</a></li>
<li class="chapter" data-level="C.2.2" data-path="data-tables.html"><a href="data-tables.html#fast-reading-and-writing"><i class="fa fa-check"></i><b>C.2.2</b> Fast Reading and Writing</a></li>
</ul></li>
<li class="chapter" data-level="C.3" data-path="data-tables.html"><a href="data-tables.html#indexing-the-major-powerhorse-of-data-tables"><i class="fa fa-check"></i><b>C.3</b> Indexing: The Major Powerhorse of Data Tables</a><ul>
<li class="chapter" data-level="C.3.1" data-path="data-tables.html"><a href="data-tables.html#i-select-observations"><i class="fa fa-check"></i><b>C.3.1</b> i: Select Observations</a></li>
<li class="chapter" data-level="C.3.2" data-path="data-tables.html"><a href="data-tables.html#j-work-with-columns"><i class="fa fa-check"></i><b>C.3.2</b> j: Work with Columns</a></li>
<li class="chapter" data-level="C.3.3" data-path="data-tables.html"><a href="data-tables.html#group-in-by"><i class="fa fa-check"></i><b>C.3.3</b> Group in <code>by</code></a></li>
</ul></li>
<li class="chapter" data-level="C.4" data-path="data-tables.html"><a href="data-tables.html#create-variables-by-reference"><i class="fa fa-check"></i><b>C.4</b> <code>:=</code>–Create variables by reference</a></li>
<li class="chapter" data-level="C.5" data-path="data-tables.html"><a href="data-tables.html#keys"><i class="fa fa-check"></i><b>C.5</b> keys</a></li>
<li class="chapter" data-level="C.6" data-path="data-tables.html"><a href="data-tables.html#resources-17"><i class="fa fa-check"></i><b>C.6</b> Resources</a></li>
</ul></li>
<li class="chapter" data-level="D" data-path="remote-server.html"><a href="remote-server.html"><i class="fa fa-check"></i><b>D</b> Using Remote Server</a><ul>
<li class="chapter" data-level="D.1" data-path="remote-server.html"><a href="remote-server.html#server-setup"><i class="fa fa-check"></i><b>D.1</b> Server Setup</a></li>
<li class="chapter" data-level="D.2" data-path="remote-server.html"><a href="remote-server.html#connecting-to-the-remote-server"><i class="fa fa-check"></i><b>D.2</b> Connecting to the Remote Server</a></li>
<li class="chapter" data-level="D.3" data-path="remote-server.html"><a href="remote-server.html#copying-files"><i class="fa fa-check"></i><b>D.3</b> Copying Files</a><ul>
<li class="chapter" data-level="D.3.1" data-path="remote-server.html"><a href="remote-server.html#scp"><i class="fa fa-check"></i><b>D.3.1</b> scp</a></li>
<li class="chapter" data-level="D.3.2" data-path="remote-server.html"><a href="remote-server.html#rsync"><i class="fa fa-check"></i><b>D.3.2</b> rsync</a></li>
<li class="chapter" data-level="D.3.3" data-path="remote-server.html"><a href="remote-server.html#graphical-frontends"><i class="fa fa-check"></i><b>D.3.3</b> Graphical Frontends</a></li>
<li class="chapter" data-level="D.3.4" data-path="remote-server.html"><a href="remote-server.html#remote-editing"><i class="fa fa-check"></i><b>D.3.4</b> Remote Editing</a></li>
</ul></li>
<li class="chapter" data-level="D.4" data-path="remote-server.html"><a href="remote-server.html#r-and-rscript"><i class="fa fa-check"></i><b>D.4</b> R and Rscript</a><ul>
<li class="chapter" data-level="D.4.1" data-path="remote-server.html"><a href="remote-server.html#graphics-output-with-no-gui"><i class="fa fa-check"></i><b>D.4.1</b> Graphics Output with No GUI</a></li>
</ul></li>
<li class="chapter" data-level="D.5" data-path="remote-server.html"><a href="remote-server.html#life-on-server"><i class="fa fa-check"></i><b>D.5</b> Life on Server</a><ul>
<li class="chapter" data-level="D.5.1" data-path="remote-server.html"><a href="remote-server.html#be-social"><i class="fa fa-check"></i><b>D.5.1</b> Be Social!</a></li>
<li class="chapter" data-level="D.5.2" data-path="remote-server.html"><a href="remote-server.html#useful-things-to-do"><i class="fa fa-check"></i><b>D.5.2</b> Useful Things to Do</a></li>
<li class="chapter" data-level="D.5.3" data-path="remote-server.html"><a href="remote-server.html#permissions-and-ownership"><i class="fa fa-check"></i><b>D.5.3</b> Permissions and ownership</a></li>
<li class="chapter" data-level="D.5.4" data-path="remote-server.html"><a href="remote-server.html#more-than-one-connection"><i class="fa fa-check"></i><b>D.5.4</b> More than One Connection</a></li>
</ul></li>
<li class="chapter" data-level="D.6" data-path="remote-server.html"><a href="remote-server.html#advanced-usage"><i class="fa fa-check"></i><b>D.6</b> Advanced Usage</a><ul>
<li class="chapter" data-level="D.6.1" data-path="remote-server.html"><a href="remote-server.html#ssh-keys-.sshconfig"><i class="fa fa-check"></i><b>D.6.1</b> ssh keys, .ssh/config</a></li>
<li class="chapter" data-level="D.6.2" data-path="remote-server.html"><a href="remote-server.html#more-about-command-line-pipes-and-shell-patterns"><i class="fa fa-check"></i><b>D.6.2</b> More about command line: pipes and shell patterns</a></li>
<li class="chapter" data-level="D.6.3" data-path="remote-server.html"><a href="remote-server.html#running-rscript-in-ssh-session"><i class="fa fa-check"></i><b>D.6.3</b> Running RScript in ssh Session</a></li>
</ul></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Technical Foundations of Informatics</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="vectors" class="section level1">
<h1><span class="header-section-number">Chapter 7</span> Vectors</h1>
<p>This chapter covers the foundational concepts for working with vectors in R. Vectors are <em>the</em> fundamental data type in R: in order to use R, you need to become comfortable with vectors. This chapter will discuss how R stores information in vectors, the way in which operations are executed in <em>vectorized</em> form, and how to extract subsets of vectors. These concepts are <strong>key to effectively programming</strong> in R.</p>
<div id="what-is-a-vector" class="section level2">
<h2><span class="header-section-number">7.1</span> What is a Vector?</h2>
<p><strong>Vectors</strong> are <em>one-dimensional ordered collections of values</em> that are all
stored in a single variable. For example, you can make a vector
<code>people</code> that contains the character strings “Sarah”, “Amit”, and
“Zhang”. Alternatively, you could make a vector <code>numbers</code> that stores
the numbers from 1 to 100. Each value in a vector is refered to as an
<strong>element</strong> of that vector; thus the <code>people</code> vector would have 3
elements, <code>"Sarah"</code>, <code>"Amit"</code>, and <code>"Zhang"</code>, and <code>numbers</code> vector
will have 100 elements. <em>Ordered</em> means that once in the vector, the
elements will remain there in the original order. If “Amit” was put
on the second place, it will remain on the second place unless explicitly moved.</p>
<p>Unfortunately, there are at least five different sometimes contradicting
definitions of what is “vector” in R. Here we focus on <strong>atomic
vectors</strong>, vectors that contain the
<a href="r-intro.html#basic-data-types">atomic data types</a>. Another different class of
vectors is
<strong>generalized vectors</strong> or <strong>lists</strong>, the topic of <a href="lists.html#lists">chapter Lists</a>.</p>
<p>Atomic vector can only contain elements of an atomic data
type—numeric, integer, character or logical. Importantly, all the
elements in a vector need to have the same. You can’t have an atomic vector whose elements include both numbers and character strings.</p>
</div>
<div id="creating-vectors" class="section level2">
<h2><span class="header-section-number">7.2</span> Creating Vectors</h2>
<p>The easiest and most universal syntax for creating vectors is to use the built in <code>c()</code> function, which is used to <strong><em>c</em></strong>_ombine_ values into a vector. The <code>c()</code> function takes in any number of <strong>arguments</strong> of the same type (separated by commas as usual), and <strong>returns</strong> a vector that contains those elements:</p>
<div class="sourceCode" id="cb51"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb51-1" data-line-number="1"><span class="co"># Use the combine (`c`) function to create a vector.</span></a>
<a class="sourceLine" id="cb51-2" data-line-number="2">people <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"Sarah"</span>, <span class="st">"Amit"</span>, <span class="st">"Zhang"</span>)</a>
<a class="sourceLine" id="cb51-3" data-line-number="3"><span class="kw">print</span>(people) <span class="co"># [1] "Sarah" "Amit" "Zhang"</span></a>
<a class="sourceLine" id="cb51-4" data-line-number="4"></a>
<a class="sourceLine" id="cb51-5" data-line-number="5">numbers <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>)</a>
<a class="sourceLine" id="cb51-6" data-line-number="6"><span class="kw">print</span>(numbers) <span class="co"># [1] 1 2 3 4 5</span></a></code></pre></div>
<p>You can use the <code>length()</code> function to determine how many <strong>elements</strong> are in a vector:</p>
<div class="sourceCode" id="cb52"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb52-1" data-line-number="1">people <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"Sarah"</span>, <span class="st">"Amit"</span>, <span class="st">"Zhang"</span>)</a>
<a class="sourceLine" id="cb52-2" data-line-number="2"><span class="kw">length</span>(people) <span class="co"># [1] 3</span></a>
<a class="sourceLine" id="cb52-3" data-line-number="3"></a>
<a class="sourceLine" id="cb52-4" data-line-number="4">numbers <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>)</a>
<a class="sourceLine" id="cb52-5" data-line-number="5"><span class="kw">length</span>(numbers) <span class="co"># [1] 5</span></a></code></pre></div>
<p>As atomic vectors can only contain same type of elements, <code>c()</code>
automatically <strong>casts</strong> (converts) one type to the other if necessary
(and if possible). For instance, when attempting to create a vector
containing number 1 and character “a”</p>
<div class="sourceCode" id="cb53"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb53-1" data-line-number="1">mix <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>, <span class="st">"a"</span>)</a>
<a class="sourceLine" id="cb53-2" data-line-number="2">mix <span class="co"># [1] "1" "a"</span></a></code></pre></div>
<p>we get a <em>character vector</em> where the number 1 was converted to a
character “1”. This is a frequent problem when reading data where some
fields contain invalid number codes.</p>
<p>There are other handy ways to create vectors. For example, the <code>seq()</code>
function mentioned in <a href="functions.html#functions">chapter 6</a> takes 2 (or more) arguments and
produces a vector of the integers between them. An <em>optional</em> third
argument specifies by which step to increment the numbers:</p>
<div class="sourceCode" id="cb54"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb54-1" data-line-number="1"><span class="co"># Make vector of numbers 1 to 90</span></a>
<a class="sourceLine" id="cb54-2" data-line-number="2">one_to_ninety <-<span class="st"> </span><span class="kw">seq</span>(<span class="dv">1</span>, <span class="dv">90</span>)</a>
<a class="sourceLine" id="cb54-3" data-line-number="3"><span class="kw">print</span>(one_to_ninety) <span class="co"># [1] 1 2 3 4 5 ...</span></a>
<a class="sourceLine" id="cb54-4" data-line-number="4"></a>
<a class="sourceLine" id="cb54-5" data-line-number="5"><span class="co"># Make vector of numbers 1 to 10, counting by 2</span></a>
<a class="sourceLine" id="cb54-6" data-line-number="6">odds <-<span class="st"> </span><span class="kw">seq</span>(<span class="dv">1</span>, <span class="dv">10</span>, <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb54-7" data-line-number="7"><span class="kw">print</span>(odds) <span class="co"># [1] 1 3 5 7 9</span></a></code></pre></div>
<ul>
<li>When you print out <code>one_to_ninety</code>, you’ll notice that in addition to the leading <code>[1]</code> that you’ve seen in all printed results, there are additional bracketed numbers at the start of each line. These bracketed numbers tells you from which element number (<strong>index</strong>, see below) that line is showing the elements of. Thus the <code>[1]</code> means that the printed line shows elements started at element number <code>1</code>, a <code>[20]</code> means that the printed line shows elements starting at element number <code>20</code>, and so on. This is to help make the output more readable, so you know where in the vector you are when looking at in a printed line of elements!</li>
</ul>
<p>As a shorthand, you can produce a sequence with the <strong>colon operator</strong> (<strong><code>a:b</code></strong>), which returns a vector <code>a</code> to <code>b</code> with the element values incrementing by <code>1</code>:</p>
<div class="sourceCode" id="cb55"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb55-1" data-line-number="1">one_to_ninety <-<span class="st"> </span><span class="dv">1</span><span class="op">:</span><span class="dv">90</span></a></code></pre></div>
<p>Another useful function that creates vectors is <code>rep()</code> that repeats
it’s first argument:</p>
<div class="sourceCode" id="cb56"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb56-1" data-line-number="1"><span class="kw">rep</span>(<span class="st">"Xi'an"</span>, <span class="dv">5</span>) <span class="co"># [1] "Xi'an" "Xi'an" "Xi'an" "Xi'an" "Xi'an"</span></a></code></pre></div>
<p><code>c()</code> can also be used to add elements to an existing vector:</p>
<div class="sourceCode" id="cb57"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb57-1" data-line-number="1"><span class="co"># Use the combine (`c()`) function to create a vector.</span></a>
<a class="sourceLine" id="cb57-2" data-line-number="2">people <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"Sarah"</span>, <span class="st">"Amit"</span>, <span class="st">"Zhang"</span>)</a>
<a class="sourceLine" id="cb57-3" data-line-number="3"></a>
<a class="sourceLine" id="cb57-4" data-line-number="4"><span class="co"># Use the `c()` function to combine the `people` vector and the name 'Josh'.</span></a>
<a class="sourceLine" id="cb57-5" data-line-number="5">more_people <-<span class="st"> </span><span class="kw">c</span>(people, <span class="st">'Josh'</span>)</a>
<a class="sourceLine" id="cb57-6" data-line-number="6"><span class="kw">print</span>(more_people) <span class="co"># [1] "Sarah" "Amit" "Zhang" "Josh"</span></a></code></pre></div>
<p>Note that <code>c()</code> retains the order of elements—“Josh” will be the
last element in the extended vector.</p>
<p>All the vector creation functions we introduced here, <code>c()</code>, <code>seq()</code> and
<code>rep()</code> are noticeably more powerful and complex than the brief
discussion above. You are encouraged to read the help pages!</p>
</div>
<div id="vector-indices" class="section level2">
<h2><span class="header-section-number">7.3</span> Vector Indices</h2>
<p>Vectors are the fundamental structure for storing collections of data. Yet you often want to only work with <em>some</em> of the data in a vector. This section will discuss a few ways that you can get a <strong>subset</strong> of elements in a vector.</p>
<p>In particular, you can refer to individual elements in a vector by their
<strong>index</strong> (more specifically <strong>numeric index</strong>), which is the number of their position in the vector. For example, in the vector:</p>
<div class="sourceCode" id="cb58"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb58-1" data-line-number="1">vowels <-<span class="st"> </span><span class="kw">c</span>(<span class="st">'a'</span>,<span class="st">'e'</span>,<span class="st">'i'</span>,<span class="st">'o'</span>,<span class="st">'u'</span>)</a></code></pre></div>
<p>The <code>'a'</code> (the first element) is at <em>index</em> 1, <code>'e'</code> (the second element) is at index 2, and so on.</p>
<p class="alert alert-info">
Note in R vector elements are indexed starting with <code>1</code> (<strong>one-based indexing</strong>). This is distinct from many other programming languages
which use <strong>zero-based indexing</strong> and so reference the first element
at index <code>0</code>.
</p>
<div id="simple-numeric-indices" class="section level3">
<h3><span class="header-section-number">7.3.1</span> Simple Numeric Indices</h3>
<p>You can retrieve a value from a vector using <strong>bracket notation</strong>: you refer to the element at a particular index of a vector by writing the name of the vector, followed by square brackets (<strong><code>[]</code></strong>) that contain the index of interest:</p>
<div class="sourceCode" id="cb59"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb59-1" data-line-number="1"><span class="co"># Create the people vector</span></a>
<a class="sourceLine" id="cb59-2" data-line-number="2">people <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"Sarah"</span>, <span class="st">"Amit"</span>, <span class="st">"Zhang"</span>)</a>
<a class="sourceLine" id="cb59-3" data-line-number="3"></a>
<a class="sourceLine" id="cb59-4" data-line-number="4"><span class="co"># access the element at index 1</span></a>
<a class="sourceLine" id="cb59-5" data-line-number="5">people[<span class="dv">1</span>] <span class="co"># [1] "Sarah"</span></a>
<a class="sourceLine" id="cb59-6" data-line-number="6"></a>
<a class="sourceLine" id="cb59-7" data-line-number="7"><span class="co"># access the element at index 2</span></a>
<a class="sourceLine" id="cb59-8" data-line-number="8">people[<span class="dv">2</span>] <span class="co"># [1] "Amit"</span></a>
<a class="sourceLine" id="cb59-9" data-line-number="9"></a>
<a class="sourceLine" id="cb59-10" data-line-number="10"><span class="co"># You can also use variables inside the brackets</span></a>
<a class="sourceLine" id="cb59-11" data-line-number="11">last_index <-<span class="st"> </span><span class="kw">length</span>(people) <span class="co"># last index is the length of the vector!</span></a>
<a class="sourceLine" id="cb59-12" data-line-number="12">people[last_index] <span class="co"># returns "Zhang"</span></a>
<a class="sourceLine" id="cb59-13" data-line-number="13"> <span class="co"># You may want to check out the `tail()` function instead!</span></a></code></pre></div>
<p>Don’t get confused by the <code>[1]</code> in the printed output—it doesn’t refer to which index you got from <code>people</code>, but what index in the <em>extracted</em> result (e.g., stored in <code>first_person</code>) is being printed!</p>
<p>If you specify an index that is <strong>out-of-bounds</strong> (e.g., greater than
the number of elements in the vector) in the square brackets, you will
get back the value <code>NA</code>, which stands for <strong>N</strong>ot <strong>A</strong>vailable. Note
that this is <em>not</em> the <em>character string</em> <code>"NA"</code>, but a specific value,
specially designed to denote missing data.</p>
<div class="sourceCode" id="cb60"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb60-1" data-line-number="1">vowels <-<span class="st"> </span><span class="kw">c</span>(<span class="st">'a'</span>,<span class="st">'e'</span>,<span class="st">'i'</span>,<span class="st">'o'</span>,<span class="st">'u'</span>)</a>
<a class="sourceLine" id="cb60-2" data-line-number="2"></a>
<a class="sourceLine" id="cb60-3" data-line-number="3"><span class="co"># Attempt to access the 10th element</span></a>
<a class="sourceLine" id="cb60-4" data-line-number="4">vowels[<span class="dv">10</span>] <span class="co"># returns NA</span></a></code></pre></div>
<p>If you specify a <strong>negative index</strong> in the square-brackets, R will return all elements <em>except</em> the (negative) index specified:</p>
<div class="sourceCode" id="cb61"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb61-1" data-line-number="1">vowels <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"a"</span>, <span class="st">"e"</span>, <span class="st">"i"</span>, <span class="st">"o"</span>, <span class="st">"u"</span>)</a>
<a class="sourceLine" id="cb61-2" data-line-number="2"></a>
<a class="sourceLine" id="cb61-3" data-line-number="3"><span class="co"># Return all elements EXCEPT that at index 2</span></a>
<a class="sourceLine" id="cb61-4" data-line-number="4">all_but_e <-<span class="st"> </span>vowels[<span class="op">-</span><span class="dv">2</span>]</a>
<a class="sourceLine" id="cb61-5" data-line-number="5"><span class="kw">print</span>(all_but_e) <span class="co"># [1] "a" "i" "o" "u"</span></a></code></pre></div>
</div>
<div id="multiple-indices" class="section level3">
<h3><span class="header-section-number">7.3.2</span> Multiple Indices</h3>
<p>Remember that in R, <strong>all atomic objects are vectors</strong>. This means that when you put a single number inside the square brackets, you’re actually putting a <em>vector with a single element in it</em> into the brackets So what you’re really doing is specifying a <strong>vector of indices</strong> that you want R to extract from the vector. As such, you can put a vector of any length inside the brackets, and R will extract <em>all</em> the elements with those indices from the vector (producing a <strong>subset</strong> of the vector elements):</p>
<div class="sourceCode" id="cb62"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb62-1" data-line-number="1"><span class="co"># Create a `colors` vector</span></a>
<a class="sourceLine" id="cb62-2" data-line-number="2">colors <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"red"</span>, <span class="st">"green"</span>, <span class="st">"blue"</span>, <span class="st">"yellow"</span>, <span class="st">"purple"</span>)</a>
<a class="sourceLine" id="cb62-3" data-line-number="3"></a>
<a class="sourceLine" id="cb62-4" data-line-number="4"><span class="co"># Vector of indices to extract</span></a>
<a class="sourceLine" id="cb62-5" data-line-number="5">indices <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">4</span>)</a>
<a class="sourceLine" id="cb62-6" data-line-number="6"></a>
<a class="sourceLine" id="cb62-7" data-line-number="7"><span class="co"># Retrieve the colors at those indices</span></a>
<a class="sourceLine" id="cb62-8" data-line-number="8">colors[indices] <span class="co"># [1] "red" "blue" "yellow"</span></a>
<a class="sourceLine" id="cb62-9" data-line-number="9"></a>
<a class="sourceLine" id="cb62-10" data-line-number="10"><span class="co"># Specify the index array anonymously</span></a>
<a class="sourceLine" id="cb62-11" data-line-number="11">colors[<span class="kw">c</span>(<span class="dv">2</span>, <span class="dv">5</span>)] <span class="co"># [1] "green" "purple"</span></a></code></pre></div>
<p>It’s very-very handy to use the <strong>colon operator</strong> to quickly specify a range of indices to extract:</p>
<div class="sourceCode" id="cb63"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb63-1" data-line-number="1">colors <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"red"</span>, <span class="st">"green"</span>, <span class="st">"blue"</span>, <span class="st">"yellow"</span>, <span class="st">"purple"</span>)</a>
<a class="sourceLine" id="cb63-2" data-line-number="2"></a>
<a class="sourceLine" id="cb63-3" data-line-number="3"><span class="co"># Retrieve values in positions 2 through 5</span></a>
<a class="sourceLine" id="cb63-4" data-line-number="4">colors[<span class="dv">2</span><span class="op">:</span><span class="dv">5</span>] <span class="co"># [1] "green" "blue" "yellow" "purple"</span></a></code></pre></div>
<p>This easily reads as <em>“a vector of the elements in positions 2 through
5”</em>.</p>
<p class="alert alert-info">
The object returned by multiple indexing (and also by a single index) is
a <strong>copy of the original</strong>, unlike in some other programming
languages. These are good news in terms of avoiding unexpected effects:
modifying the returned copy does not affect the original.
However, copying large objects may be costly and make your code slow and sluggish.
</p>
</div>
<div id="logical-indexing" class="section level3">
<h3><span class="header-section-number">7.3.3</span> Logical Indexing</h3>
<p>In the above section, you used a vector of indices (<em>numeric</em> values) to
retrieve a subset of elements from a vector. Alternatively, you can put a <strong>vector of logical values</strong> inside the square brackets to specify which ones you want to extract (<code>TRUE</code> in the <em>corresponding position</em> means extract, <code>FALSE</code> means don’t extract):</p>
<div class="sourceCode" id="cb64"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb64-1" data-line-number="1"><span class="co"># Create a vector of shoe sizes</span></a>
<a class="sourceLine" id="cb64-2" data-line-number="2">shoe_sizes <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">7</span>, <span class="fl">6.5</span>, <span class="dv">4</span>, <span class="dv">11</span>, <span class="dv">8</span>)</a>
<a class="sourceLine" id="cb64-3" data-line-number="3"></a>
<a class="sourceLine" id="cb64-4" data-line-number="4"><span class="co"># Vector of elements to extract</span></a>
<a class="sourceLine" id="cb64-5" data-line-number="5">filter <-<span class="st"> </span><span class="kw">c</span>(<span class="ot">TRUE</span>, <span class="ot">FALSE</span>, <span class="ot">FALSE</span>, <span class="ot">TRUE</span>, <span class="ot">TRUE</span>)</a>
<a class="sourceLine" id="cb64-6" data-line-number="6"></a>
<a class="sourceLine" id="cb64-7" data-line-number="7"><span class="co"># Extract every element in an index that is TRUE</span></a>
<a class="sourceLine" id="cb64-8" data-line-number="8">shoe_sizes[filter] <span class="co"># [1] 7 11 8</span></a></code></pre></div>
<p>R will go through the boolean vector and extract every item at the
position that is <code>TRUE</code>. In the example above, since <code>filter</code> is <code>TRUE</code> and indices 1, 4, and 5, then <code>shoe_sizes[filter]</code> returns a vector with the elements from indices 1, 4, and 5.</p>
<p>This may seem a bit strange, but it is actually incredibly powerful because it lets you select elements from a vector that <em>meet a certain criteria</em> (called <strong>filtering</strong>). You perform this <em>filtering operation</em> by first creating a vector of boolean values that correspond with the indices meeting that criteria, and then put that filter vector inside the square brackets:</p>
<div class="sourceCode" id="cb65"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb65-1" data-line-number="1"><span class="co"># Create a vector of shoe sizes</span></a>
<a class="sourceLine" id="cb65-2" data-line-number="2">shoe_sizes <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">7</span>, <span class="fl">6.5</span>, <span class="dv">4</span>, <span class="dv">11</span>, <span class="dv">8</span>)</a>
<a class="sourceLine" id="cb65-3" data-line-number="3"></a>
<a class="sourceLine" id="cb65-4" data-line-number="4"><span class="co"># Create a boolean vector that indicates if a shoe size is greater than 6.5</span></a>
<a class="sourceLine" id="cb65-5" data-line-number="5">shoe_is_big <-<span class="st"> </span>shoe_sizes <span class="op">></span><span class="st"> </span><span class="fl">6.5</span> <span class="co"># T, F, F, T, T</span></a>
<a class="sourceLine" id="cb65-6" data-line-number="6"></a>
<a class="sourceLine" id="cb65-7" data-line-number="7"><span class="co"># Use the `shoe_is_big` vector to select large shoes</span></a>
<a class="sourceLine" id="cb65-8" data-line-number="8">big_shoes <-<span class="st"> </span>shoe_sizes[shoe_is_big] <span class="co"># returns 7, 11, 8</span></a></code></pre></div>
<p>There is often little reason to explicitly create the index vector
<code>shoe_is_big</code>. You can combine the second and third lines of code into
a single statement with anonymous index vector:</p>
<div class="sourceCode" id="cb66"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb66-1" data-line-number="1"><span class="co"># Create a vector of shoe sizes</span></a>
<a class="sourceLine" id="cb66-2" data-line-number="2">shoe_sizes <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">7</span>, <span class="fl">6.5</span>, <span class="dv">4</span>, <span class="dv">11</span>, <span class="dv">8</span>)</a>
<a class="sourceLine" id="cb66-3" data-line-number="3"></a>
<a class="sourceLine" id="cb66-4" data-line-number="4"><span class="co"># Select shoe sizes that are greater than 6.5</span></a>
<a class="sourceLine" id="cb66-5" data-line-number="5">shoe_sizes[shoe_sizes <span class="op">></span><span class="st"> </span><span class="fl">6.5</span>] <span class="co"># returns 7, 11, 8</span></a></code></pre></div>
<p>You can think of the this statement as saying “shoe_sizes <strong>where</strong>
shoe_sizes is greater than 6.5”. This is a valid statement because the
logical expression inside of the square-brackets (<code>shoe_sizes > 6.5</code>) is
evaluated first, producing an anonymos boolean vector which is then used to filter the <code>shoe_sizes</code> vector.</p>
<p>This kind of filtering is immensely popular in real-life applications.</p>
</div>
<div id="named-vectors-and-character-indexing" class="section level3">
<h3><span class="header-section-number">7.3.4</span> Named Vectors and Character Indexing</h3>
<p>All the vectors we created above where created without names. But
vector elements can have names, and given they have names, we can access
these using the names. There are two ways to create <strong>named vectors</strong>.</p>
<p>First, we can add names when creating vectors with <code>c()</code> function:</p>
<div class="sourceCode" id="cb67"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb67-1" data-line-number="1">param <-<span class="st"> </span><span class="kw">c</span>(<span class="dt">gamma=</span><span class="dv">3</span>, <span class="dt">alpha=</span><span class="fl">1.7</span>, <span class="st">"c-2"</span>=<span class="op">-</span><span class="fl">1.33</span>)</a>
<a class="sourceLine" id="cb67-2" data-line-number="2">param</a>
<a class="sourceLine" id="cb67-3" data-line-number="3"></a>
<a class="sourceLine" id="cb67-4" data-line-number="4"><span class="co">## gamma alpha c-2 </span></a>
<a class="sourceLine" id="cb67-5" data-line-number="5"><span class="co">## 3.00 1.70 -1.33 </span></a></code></pre></div>
<p>This creates a numeric vector of length 3 where each element has a
name. Note that we have to quote the names, such as “c-2”, that are not
valid R variable names. Note also that the printout differs from that
of unnamed vectors, in particular the index position (<code>[1]</code>) is not
printed.</p>
<p>Alternatively, we can set names to an already existing vector using the
<code>names()</code>
function:<a href="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a></p>
<div class="sourceCode" id="cb68"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb68-1" data-line-number="1">numbers <-<span class="st"> </span><span class="dv">1</span><span class="op">:</span><span class="dv">5</span></a>
<a class="sourceLine" id="cb68-2" data-line-number="2"><span class="kw">names</span>(numbers) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"A"</span>, <span class="st">"B"</span>, <span class="st">"C"</span>, <span class="st">"D"</span>, <span class="st">"E"</span>)</a>
<a class="sourceLine" id="cb68-3" data-line-number="3">numbers</a>
<a class="sourceLine" id="cb68-4" data-line-number="4"></a>
<a class="sourceLine" id="cb68-5" data-line-number="5"><span class="co">## A B C D E </span></a>
<a class="sourceLine" id="cb68-6" data-line-number="6"><span class="co">## 1 2 3 4 5 </span></a></code></pre></div>
<p>Now when we have a named vector, we can access it’s elements by names.
For instance</p>
<div class="sourceCode" id="cb69"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb69-1" data-line-number="1">numbers[<span class="st">"C"</span>]</a>
<a class="sourceLine" id="cb69-2" data-line-number="2"><span class="co">## C</span></a>
<a class="sourceLine" id="cb69-3" data-line-number="3"><span class="co">## 3</span></a>
<a class="sourceLine" id="cb69-4" data-line-number="4">numbers[<span class="kw">c</span>(<span class="st">"D"</span>, <span class="st">"B"</span>)]</a>
<a class="sourceLine" id="cb69-5" data-line-number="5"><span class="co">## D B </span></a>
<a class="sourceLine" id="cb69-6" data-line-number="6"><span class="co">## 4 2 </span></a></code></pre></div>
<p>Note that in the latter case the names <code>"B"</code> and <code>"D"</code> are in “wrong
order”, i.e. not in the same order as they are in the vector <code>numbers</code>.
However, this works just fine, the elements are extracted in the order
they are specified in the index (This is only possible with character
and numeric indices, logical index can only extract elements in the
“right” order.)</p>
<p>While most vectors we encounter in this book gain little by
names, exactly the same approach also applies to lists and data frames
where character indexing is one of the important workhorses.</p>
<p>Another important use case of named vectors in R are a substitute of <strong>maps</strong>
(aka <strong>dictionaries</strong>). Maps are just lookup tables where we can
find a value that corresponds to a value of another element in the
table. For instance, the example above found values that correspond to
the names <code>"D"</code> and <code>"B"</code>.</p>
</div>
</div>
<div id="modifying-vectors" class="section level2">
<h2><span class="header-section-number">7.4</span> Modifying Vectors</h2>
<p>Indexing can also be used to modify elements within the vector. To do this, put the extracted <em>subset</em> on the <strong>left-hand side</strong> of the assignment operator, and then assign the element a new value:</p>
<div class="sourceCode" id="cb70"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb70-1" data-line-number="1"><span class="co"># Create a vector of school supplies</span></a>
<a class="sourceLine" id="cb70-2" data-line-number="2">school_supplies <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"Backpack"</span>, <span class="st">"Laptop"</span>, <span class="st">"Pen"</span>)</a>
<a class="sourceLine" id="cb70-3" data-line-number="3"></a>
<a class="sourceLine" id="cb70-4" data-line-number="4"><span class="co"># Replace 'Pen' (element at index 3) with 'Pencil'</span></a>
<a class="sourceLine" id="cb70-5" data-line-number="5">school_supplies[<span class="dv">3</span>] <-<span class="st"> "Pencil"</span></a></code></pre></div>
<p>And of course, there’s no reason that you can’t select multiple elements on the left-hand side, and assign them multiple values. The assignment operator is <em>vectorized</em>!</p>
<div class="sourceCode" id="cb71"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb71-1" data-line-number="1"><span class="co"># Create a vector of school supplies</span></a>
<a class="sourceLine" id="cb71-2" data-line-number="2">school_supplies <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"Backpack"</span>, <span class="st">"Laptop"</span>, <span class="st">"Pen"</span>)</a>
<a class="sourceLine" id="cb71-3" data-line-number="3"></a>
<a class="sourceLine" id="cb71-4" data-line-number="4"><span class="co"># Replace 'Laptop' with 'Tablet', and 'Pen' with 'Pencil'</span></a>
<a class="sourceLine" id="cb71-5" data-line-number="5">school_supplies[<span class="kw">c</span>(<span class="dv">2</span>, <span class="dv">3</span>)] <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"Tablet"</span>, <span class="st">"Pencil"</span>)</a></code></pre></div>
<p>If you vector has names, you can use character indexing in exactly the
same way.</p>
<p>Logical indexing offer some very powerful possibilities. Imagine you
had a vector of values in which you wanted to replace all numbers
greater that 10 with the number 10 (to “cap” the values). We can
achieve with an one-liner:</p>
<div class="sourceCode" id="cb72"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb72-1" data-line-number="1"><span class="co"># Element of values</span></a>
<a class="sourceLine" id="cb72-2" data-line-number="2">v1 <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">5</span>, <span class="dv">55</span>, <span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">11</span>, <span class="dv">4</span>, <span class="dv">27</span>)</a>
<a class="sourceLine" id="cb72-3" data-line-number="3"></a>
<a class="sourceLine" id="cb72-4" data-line-number="4"><span class="co"># Replace all values greater than 10 with 10</span></a>
<a class="sourceLine" id="cb72-5" data-line-number="5">v1[v1 <span class="op">></span><span class="st"> </span><span class="dv">10</span>] <-<span class="st"> </span><span class="dv">10</span> <span class="co"># returns 1, 5, 10, 1, 3, 10, 4, 10</span></a></code></pre></div>
<p>In this example, we first compute the logical index of “too large”
values by <code>v1 > 10</code>, and thereafter assign the value 10 to all these
elements in vector v1. Replacing a numeric vector by the absolute
values of the elements can be done in a similar fashion:</p>
<div class="sourceCode" id="cb73"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb73-1" data-line-number="1">v <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>,<span class="op">-</span><span class="dv">1</span>,<span class="dv">2</span>,<span class="op">-</span><span class="dv">2</span>)</a>
<a class="sourceLine" id="cb73-2" data-line-number="2">v[v <span class="op"><</span><span class="st"> </span><span class="dv">0</span>] <-<span class="st"> </span><span class="op">-</span>v[v <span class="op"><</span><span class="st"> </span><span class="dv">0</span>]</a>
<a class="sourceLine" id="cb73-3" data-line-number="3">v <span class="co"># [1] 1 1 2 2</span></a></code></pre></div>
<p>As a first step we find the logical index of the negative elements of
<code>v</code>: <code>v < 0</code>. Next, we flip the sign of these elements in <code>v</code> by
replacing these with <code>-v[v < 0]</code>.</p>
</div>
<div id="vectorized-operations" class="section level2">
<h2><span class="header-section-number">7.5</span> Vectorized Operations</h2>
<p>Many R operators and functions are optimized for vectors, i.e. when fed
a vector, they work on all elements of that vector. These operations
are usually very fast and efficient.</p>
<div id="vectorized-operators" class="section level3">
<h3><span class="header-section-number">7.5.1</span> Vectorized Operators</h3>
<p>When performing operations (such as mathematical operations <code>+</code>, <code>-</code>, etc.) on vectors, the operation is applied to vector elements <strong>member-wise</strong>. This means that each element from the first vector operand is modified by the element in the <strong>same corresponding position</strong> in the second vector operand, in order to determine the value <em>at the corresponding position</em> of the resulting vector. E.g., if you want to add (<code>+</code>) two vectors, then the value of the first element in the result will be the sum (<code>+</code>) of the first elements in each vector, the second element in the result will be the sum of the second elements in each vector, and so on.</p>
<div class="sourceCode" id="cb74"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb74-1" data-line-number="1"><span class="co"># Create two vectors to combine</span></a>
<a class="sourceLine" id="cb74-2" data-line-number="2">v1 <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">1</span>)</a>
<a class="sourceLine" id="cb74-3" data-line-number="3">v2 <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>)</a>
<a class="sourceLine" id="cb74-4" data-line-number="4"></a>
<a class="sourceLine" id="cb74-5" data-line-number="5"><span class="co"># Create arithmetic combinations of the vectors</span></a>
<a class="sourceLine" id="cb74-6" data-line-number="6">v1 <span class="op">+</span><span class="st"> </span>v2 <span class="co"># returns 2, 3, 4, 5, 6</span></a>
<a class="sourceLine" id="cb74-7" data-line-number="7">v1 <span class="op">-</span><span class="st"> </span>v2 <span class="co"># returns 0, -1, -2, -3, -4</span></a>
<a class="sourceLine" id="cb74-8" data-line-number="8">v1 <span class="op">*</span><span class="st"> </span>v2 <span class="co"># returns 1, 2, 3, 4, 5</span></a>
<a class="sourceLine" id="cb74-9" data-line-number="9">v1 <span class="op">/</span><span class="st"> </span>v2 <span class="co"># returns 1, .5, .33, .25, .2</span></a>
<a class="sourceLine" id="cb74-10" data-line-number="10"></a>
<a class="sourceLine" id="cb74-11" data-line-number="11"><span class="co"># Add a vector to itself (why not?)</span></a>
<a class="sourceLine" id="cb74-12" data-line-number="12">v3 <-<span class="st"> </span>v2 <span class="op">+</span><span class="st"> </span>v2 <span class="co"># returns 2, 4, 6, 8, 10</span></a>
<a class="sourceLine" id="cb74-13" data-line-number="13"></a>
<a class="sourceLine" id="cb74-14" data-line-number="14"><span class="co"># Perform more advanced arithmetic!</span></a>
<a class="sourceLine" id="cb74-15" data-line-number="15">v4 <-<span class="st"> </span>(v1 <span class="op">+</span><span class="st"> </span>v2) <span class="op">/</span><span class="st"> </span>(v1 <span class="op">+</span><span class="st"> </span>v1) <span class="co"># returns 1, 1.5, 2, 2.5, 3</span></a></code></pre></div>
</div>
<div id="vectorized-functions" class="section level3">
<h3><span class="header-section-number">7.5.2</span> Vectorized Functions</h3>
<blockquote>
<p><em>Vectors In, Vector Out</em></p>
</blockquote>
<p>Because all atomic objects are vectors, it means that pretty much every
function you’ve used so far has actually applied to vectors, not just to
single values. These are referred to as <strong>vectorized functions</strong>, and
will run significantly faster than non-vector approaches. You’ll find
that functions work the same way for vectors as they do for single
values, because single values are just instances of vectors! For
instance, we can use <code>paste()</code> to
concatenate the elements of two character vectors:</p>
<div class="sourceCode" id="cb75"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb75-1" data-line-number="1">colors <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"Green"</span>, <span class="st">"Blue"</span>)</a>
<a class="sourceLine" id="cb75-2" data-line-number="2">spaces <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"sky"</span>, <span class="st">"grass"</span>)</a>
<a class="sourceLine" id="cb75-3" data-line-number="3"></a>
<a class="sourceLine" id="cb75-4" data-line-number="4"><span class="co"># Note: look up the `paste()` function if it's not familiar!</span></a>
<a class="sourceLine" id="cb75-5" data-line-number="5"><span class="kw">paste</span>(colors, spaces) <span class="co"># "Green sky", "Blue grass"</span></a></code></pre></div>
<p>Notice the same <em>member-wise</em> combination is occurring: the <code>paste()</code> function is applied to the first elements, then to the second elements, and so on.</p>
<ul>
<li><p><em>Fun fact:</em> The mathematical operators (e.g., <code>+</code>) are actually functions in R that take 2 arguments (the operands). The mathematical notation we’re used to using is just a shortcut.</p>
<div class="sourceCode" id="cb76"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb76-1" data-line-number="1"><span class="co"># these two lines of code are the same:</span></a>
<a class="sourceLine" id="cb76-2" data-line-number="2">x <-<span class="st"> </span><span class="dv">2</span> <span class="op">+</span><span class="st"> </span><span class="dv">3</span> <span class="co"># add 2 and 3</span></a>
<a class="sourceLine" id="cb76-3" data-line-number="3">x <-<span class="st"> '+'</span>(<span class="dv">2</span>, <span class="dv">3</span>) <span class="co"># add 2 and 3</span></a></code></pre></div></li>
</ul>
<p>For another example consider the <code>round()</code> function described in the previous chapter. This function rounds the given argument to the nearest whole number (or number of decimal places if specified).</p>
<div class="sourceCode" id="cb77"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb77-1" data-line-number="1"><span class="co"># round number to 1 decimal place</span></a>
<a class="sourceLine" id="cb77-2" data-line-number="2"><span class="kw">round</span>(<span class="fl">1.67</span>, <span class="dv">1</span>) <span class="co"># returns 1.6</span></a></code></pre></div>
<p>But recall that the <code>1.6</code> in the above example is <em>actually a vector of
length 1</em>. If we instead pass a longer vector as an argument, the function will perform the same rounding on each element in the vector.</p>
<div class="sourceCode" id="cb78"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb78-1" data-line-number="1"><span class="co"># Create a vector of numbers</span></a>
<a class="sourceLine" id="cb78-2" data-line-number="2">nums <-<span class="st"> </span><span class="kw">c</span>(<span class="fl">3.98</span>, <span class="dv">8</span>, <span class="fl">10.8</span>, <span class="fl">3.27</span>, <span class="fl">5.21</span>)</a>
<a class="sourceLine" id="cb78-3" data-line-number="3"></a>
<a class="sourceLine" id="cb78-4" data-line-number="4"><span class="co"># Perform the vectorized operation</span></a>
<a class="sourceLine" id="cb78-5" data-line-number="5"><span class="kw">round</span>(nums, <span class="dv">1</span>) <span class="co"># [1] 4.0 8.0 10.8 3.3 5.2</span></a></code></pre></div>
<p>This vectorization process is <strong><em>extremely powerful</em></strong>, and is a significant factor in what makes R an efficient language for working with large data sets (particularly in comparison to languages that require explicit iteration through elements in a collection). Thus to write really effective R code, you’ll need to be comfortable applying functions to vectors of data, and getting vectors of data back as results.</p>
<p class="alert alert-info">
Just remember: <em>when you use a vectorized function on a vector, you’re using that function <strong>on each item</strong> in the vector</em>!
</p>
</div>
<div id="recycling" class="section level3">
<h3><span class="header-section-number">7.5.3</span> Recycling</h3>
<p>Above we saw a number of vectorized operations, where similar operations
were applied to elements of two vectors member-wise. However, what
happens if the two vectors are of unequal length?
<strong>Recycling</strong> refers to what R does in cases when there are an unequal number of elements in two operand vectors. If R is tasked with performing a vectorized operation with two vectors of unequal length, it will reuse (<em>recycle</em>) elements from the shorter vector. For example:</p>
<div class="sourceCode" id="cb79"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb79-1" data-line-number="1"><span class="co"># Create vectors to combine</span></a>
<a class="sourceLine" id="cb79-2" data-line-number="2">v1 <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">5</span>, <span class="dv">8</span>)</a>
<a class="sourceLine" id="cb79-3" data-line-number="3">v2 <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb79-4" data-line-number="4"></a>
<a class="sourceLine" id="cb79-5" data-line-number="5"><span class="co"># Add vectors</span></a>
<a class="sourceLine" id="cb79-6" data-line-number="6">v1 <span class="op">+</span><span class="st"> </span>v2 <span class="co"># [1] 2 5 6 10</span></a></code></pre></div>
<p>In this example, R first combined the elements in the first position of
each vector (<code>1+1=2</code>). Then, it combined elements from the second
position (<code>3+2=5</code>). When it got to the third element of <code>v1</code> it run out
of elements of <code>v2</code>, so it went back to the <strong>beginning</strong> of <code>v2</code> to
select a value, yielding <code>5+1=6</code>. Finally, it combined the 4th element
of <code>v1</code> (8) with the second element of <code>v2</code> (2) to get 10.</p>
<p>If the longer object length is not a multiple of shorter object length,
R will issue a warning, notifying you that the lengths do not match.
This warning doesn’t necessarily mean you did something wrong although
in practice this tends to be the case.</p>
</div>
<div id="r-is-a-vectorized-world" class="section level3">
<h3><span class="header-section-number">7.5.4</span> R Is a Vectorized World</h3>
<p>Actually we have already met many more examples of recycling and
vectorized functions above. For
instance, in the case of finding big shoes with</p>
<div class="sourceCode" id="cb80"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb80-1" data-line-number="1">shoe_sizes <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">7</span>, <span class="fl">6.5</span>, <span class="dv">4</span>, <span class="dv">11</span>, <span class="dv">8</span>)</a>
<a class="sourceLine" id="cb80-2" data-line-number="2">shoe_sizes[shoe_sizes <span class="op">></span><span class="st"> </span><span class="fl">6.5</span>]</a></code></pre></div>
<p>we first recycle the length-one vector <code>6.5</code> five times to match it with
the shoe size vector <code>c(7, 6.5, 4, 11, 8)</code>. Afterwards we use the
vectorized operator <code>></code> (or actually the function <code>">()"</code>) to compare
each of the shoe sizes with the value 6.5. The result is a logical
vector of length 5.</p>
<p>This is also what happens if you add a vector and a “regular” single value (a <strong>scalar</strong>):</p>
<div class="sourceCode" id="cb81"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb81-1" data-line-number="1"><span class="co"># create vector of numbers 1 to 5</span></a>
<a class="sourceLine" id="cb81-2" data-line-number="2">v1 <-<span class="st"> </span><span class="dv">1</span><span class="op">:</span><span class="dv">5</span></a>
<a class="sourceLine" id="cb81-3" data-line-number="3">v1 <span class="op">+</span><span class="st"> </span><span class="dv">4</span> <span class="co"># add scalar to vector</span></a>
<a class="sourceLine" id="cb81-4" data-line-number="4"><span class="co">## [1] 5 6 7 8 9</span></a></code></pre></div>
<p>As you can see (and probably expected), the operation added <code>4</code> to every
element in the vector. The reason this sensible behavior occurs is
because all atomic objects are vectors. Even when you thought you were
creating a single value (a scalar), you were actually just creating a
vector with a single element (length 1). When you create a variable
storing the number <code>7</code> (with <code>x <- 7</code>), R creates a vector of length 1
with the number <code>7</code> as that single element:</p>
<div class="sourceCode" id="cb82"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb82-1" data-line-number="1"><span class="co"># Create a vector of length 1 in a variable x</span></a>
<a class="sourceLine" id="cb82-2" data-line-number="2">x <-<span class="st"> </span><span class="dv">7</span> <span class="co"># equivalent to `x <- c(7)`</span></a></code></pre></div>
<ul>
<li><p>This is why R prints the <code>[1]</code> in front of all results: it’s telling you that it’s showing a vector (which happens to have 1 element) starting at element number 1.</p></li>
<li><p>This is also why you can’t use the <code>length()</code> function to get the
length of a character string; it just returns the length of the array
containing that string (<code>1</code>). Instead, use the <code>nchar()</code> function to
get the number of characters in each element in a character vector.</p></li>
</ul>
<p>Thus when you add a “scalar” such as <code>4</code> to a vector, what you’re really doing is adding a vector with a single element <code>4</code>. As such the same <em>recycling</em> principle applies, and that single element is “recycled” and applied to each element of the first operand.</p>
<p class="alert alert-info">
Note: here we are implicitly using the word <em>vector</em> in two different
meanings. The one is a way R stores objects (atomic vector), the other
is <em>vector</em> in the mathematical sense, as the opposite to <em>scalar</em>.
Similar confusion also occurs with matrices. Matrices as mathematical
objects are distinct from vectors (and scalars). In R they are stored
as vectors, and treated as matrices in dedicated matrix operations only.
</p>
<p>Finally, you should also know that there are many kinds of objects in
R that are not vectors. These include functions, and many other more
“exotic” objects.</p>
</div>
</div>
<div id="resources-6" class="section level2 unnumbered">
<h2>Resources</h2>
<ul>
<li><a href="http://www.r-tutor.com/r-introduction/vector">R Tutorial: Vectors</a></li>
</ul>
</div>
</div>
<div class="footnotes">
<hr />
<ol start="1">
<li id="fn1"><p>Strictly speaking, this is <code>names()<-</code> function, the assignment function that sets the names, in contrast to the <code>names()</code> function that extracts names from an object.<a href="vectors.html#fnref1" class="footnote-back">↩</a></p></li>
</ol>
</div>
</section>
</div>
</div>
</div>
<a href="functions.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="lists.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": true,
"facebook": false,
"twitter": false,
"google": false,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["github", "facebook", "twitter", "google"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/info201/book/edit/master/vectors.Rmd",
"text": "Edit"
},
"history": {
"link": null,
"text": null
},
"download": null,
"toc": {
"collapse": "section",
"scroll_highlight": true
}
});
});
</script>
</body>
</html>