forked from regan008/8500-Worksheets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-DataStructures.html
1465 lines (1439 loc) · 105 KB
/
2-DataStructures.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
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
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.2.269">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Worksheet 2: Data Structures</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; 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 {
pre > code.sourceCode > span > a:first-child::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 { color: #008000; } /* 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 { color: #008000; font-weight: bold; } /* 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>
<script src="2-DataStructures_files/libs/clipboard/clipboard.min.js"></script>
<script src="2-DataStructures_files/libs/quarto-html/quarto.js"></script>
<script src="2-DataStructures_files/libs/quarto-html/popper.min.js"></script>
<script src="2-DataStructures_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="2-DataStructures_files/libs/quarto-html/anchor.min.js"></script>
<link href="2-DataStructures_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="2-DataStructures_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="2-DataStructures_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="2-DataStructures_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="2-DataStructures_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
</head>
<body class="fullcontent">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">Worksheet 2: Data Structures</h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<p><em>This is the second in a series of worksheets for History 8510 at Clemson University. The goal of these worksheets is simple: practice, practice, practice. The worksheet introduces concepts and techniques and includes prompts for you to practice in this interactive document. When you are finished, you should change the author name (above), knit your document to a pdf, and upload it to canvas.</em></p>
<section id="subsetting-loops-control-structures-and-functions" class="level2">
<h2 class="anchored" data-anchor-id="subsetting-loops-control-structures-and-functions">Subsetting, Loops & Control Structures, and Functions</h2>
<section id="subsetting" class="level3">
<h3 class="anchored" data-anchor-id="subsetting">Subsetting</h3>
<p>Subsetting is the process of retrieving just the parts (a subset) of a large vector, list, or data frame. R has numerous operators for subsetting information or data structures. They allow you to perform complex operations with a small amount of code but can be hard to master because subsetting operators (<code>[[</code>, <code>[</code>, and <code>$</code>) interact differently with different vector types.</p>
<p>We’ll be talking about vectors throughout the first part of this worksheet and it might be useful to refresh our memories about what a vector is.</p>
<blockquote class="blockquote">
<p><em>A vector is a list of variables, and the simplest data structure in R. A vector consists of a collection of numbers, arithmetic expressions, logical values or character strings for example.</em></p>
</blockquote>
<section id="selecting-multiple-elements" class="level4">
<h4 class="anchored" data-anchor-id="selecting-multiple-elements">Selecting Multiple Elements</h4>
<p>Lets start by creating a simple vector called x.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">c</span>(<span class="fl">15.4</span>, <span class="dv">25</span>, <span class="dv">2</span>, <span class="fl">8.35</span>, <span class="dv">5</span>, <span class="dv">383</span>, <span class="fl">10.2</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>If you type <code>x</code> into your console, you will see that all of the values are printed out and preceded by a <code>[1]</code>. What the <code>[1]</code> refers to is that first number, 15.4. Its position in the list is 1. Each number in our list has an index number and that is how you retrieve specific positions in this vector.</p>
<p>For example, we can use a positive integer to return elements at specific positions. Lets grab the value in the 3rd and 5th position.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>x[<span class="fu">c</span>(<span class="dv">3</span>,<span class="dv">5</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2 5</code></pre>
</div>
</div>
<p>We can also use functions to manipulate our vector. Here we use <code>order()</code> to print the values contained in the vector in order.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>x[<span class="fu">order</span>(x)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.00 5.00 8.35 10.20 15.40 25.00 383.00</code></pre>
</div>
</div>
<p>Duplicate indices will return duplicate values.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>x[<span class="fu">c</span>(<span class="dv">3</span>,<span class="dv">3</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2 2</code></pre>
</div>
</div>
<ol class="example" type="1">
<li>Create your own vector and return three values using the subsetting method above.</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>k <span class="ot"><-</span> <span class="fu">c</span>(<span class="dv">12</span>, <span class="dv">7</span>, <span class="dv">8</span>, <span class="dv">9</span>)</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>k[<span class="fu">c</span>(<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 7 8 9</code></pre>
</div>
</div>
<p>Negative integers allow us to exclude values at certain positions.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>x[<span class="sc">-</span><span class="fu">c</span>(<span class="dv">3</span>, <span class="dv">1</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 25.00 8.35 5.00 383.00 10.20</code></pre>
</div>
</div>
<ol start="2" class="example" type="1">
<li>What happened here? Describe this in your own words.</li>
</ol>
<blockquote class="blockquote">
<p>Excluded the values at the 3rd (2) and 1st (15.4) position and listed the values in the origial order without the excluded values.</p>
</blockquote>
<p>You can use either positive or negative integers to subset a vector but you <strong>cannot mix the two</strong>.</p>
<p>We can assign logical values to each value in a vector and use those to subset the data. Logical vectors select elements where the corresponding logical value is <code>TRUE</code>. Remember, we created a vector earlier and assigned it to x. Now, below, we assign logical values to each of the values in that vector. We’re doing this by hand here, but you can imagine a scenario down the road where you use this technique to apply <code>TRUE</code> or <code>FALSE</code> values to a huge dataset dependent on some principal. When we run this, only the <code>TRUE</code> values are printed out.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="co">#create a vector </span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>x[<span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>, <span class="cn">TRUE</span>, <span class="cn">TRUE</span>, <span class="cn">FALSE</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 15.40 2.00 8.35 383.00</code></pre>
</div>
</div>
<p>We can also subset to get values that match a particular criteria. Below, each item in the vector is tested against this proposition and it returns the values that are <code>TRUE</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>x[x <span class="sc">></span> <span class="dv">7</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 15.40 25.00 8.35 383.00 10.20</code></pre>
</div>
</div>
<ol start="3" class="example" type="1">
<li>What is going on in each of the lines above? Describe it in your own words.</li>
</ol>
<blockquote class="blockquote">
<p>In the assigning values example each of the first 5 variables is either assigned a TRUE or FALSE logical value. If the logical value is TRUE the corresponding value will be displayed when the code is run. The last two values that were not assigned a logical value are not displayed.</p>
</blockquote>
<blockquote class="blockquote">
<p>In the x[x>7] each value is tested against the equation and if the answer returned is TRUE the corresponding value will be displayed.</p>
</blockquote>
<p>Nothing returns the original vector. This isn’t that useful here but can come in handy when you have more complex structures like matrices and arrays.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>x[]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 15.40 25.00 2.00 8.35 5.00 383.00 10.20</code></pre>
</div>
</div>
<p>If your vector is named, you can also use character vectors to return elements with matching names.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>(y <span class="ot"><-</span> <span class="fu">setNames</span>(x, letters[<span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>]))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> a b c d <NA> <NA> <NA>
15.40 25.00 2.00 8.35 5.00 383.00 10.20 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>y[<span class="fu">c</span>(<span class="st">"d"</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> d
8.35 </code></pre>
</div>
</div>
<ol start="4" class="example" type="1">
<li>What happened here? Explain this in your own words.</li>
</ol>
<blockquote class="blockquote">
<p>Used the setNames function to set letter names for the first four values in dataset x and assigned the returned object (the named dataset) to y. Then it used c (which is the combine function) to call the value assigned to “d” which returned 8.35 and preserved its name (d).</p>
</blockquote>
</section>
<section id="matrices" class="level4">
<h4 class="anchored" data-anchor-id="matrices">Matrices</h4>
<p>We can subset higher dimensional structures, like matrices, too. First lets define a matrix. In R, <strong>a matrix</strong> is a collection of elements of the same data type (numeric, character, or logical) arranged into a fixed number of rows and columns. Since you are only working with rows and columns, a matrix is called two dimensional. To create a matrix you can use the <code>matrix()</code> function.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">matrix</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">9</span>, <span class="at">byrow =</span> <span class="cn">TRUE</span>, <span class="at">nrow =</span> <span class="dv">3</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9</code></pre>
</div>
</div>
<p>In the <code>matrix()</code> function:</p>
<ul>
<li>The first argument is the collection of elements that R will arrange into the rows and columns of the matrix. Here, we use 1:9 which is a shortcut for c(1, 2, 3, 4, 5, 6, 7, 8, 9).</li>
<li>The argument byrow indicates that the matrix is filled by the rows. If we want the matrix to be filled by the columns, we just place byrow = FALSE.</li>
<li>The third argument nrow indicates that the matrix should have three rows.</li>
</ul>
<ol start="5" class="example" type="1">
<li>You try now, create a matrix that had five rows with numbers that range from 25 to 49.</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="fu">matrix</span>(<span class="dv">25</span><span class="sc">:</span><span class="dv">49</span>, <span class="at">byrow =</span> <span class="cn">TRUE</span>, <span class="at">nrow =</span> <span class="dv">5</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [,1] [,2] [,3] [,4] [,5]
[1,] 25 26 27 28 29
[2,] 30 31 32 33 34
[3,] 35 36 37 38 39
[4,] 40 41 42 43 44
[5,] 45 46 47 48 49</code></pre>
</div>
</div>
<ol start="6" class="example" type="1">
<li>Can you create a matrix that has 3 rows with only even numbers from 2 to 18?</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="fu">matrix</span>(<span class="fu">seq</span>(<span class="dv">2</span>, <span class="dv">18</span>, <span class="at">by =</span> <span class="dv">2</span>), <span class="at">byrow =</span> <span class="cn">TRUE</span>, <span class="at">nrow =</span> <span class="dv">3</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [,1] [,2] [,3]
[1,] 2 4 6
[2,] 8 10 12
[3,] 14 16 18</code></pre>
</div>
</div>
<section id="subsetting-matricies" class="level5">
<h5 class="anchored" data-anchor-id="subsetting-matricies">Subsetting Matricies</h5>
<p>The most common way to subset matrices and arrays is to use a simple generalization of the same subsetting method we used for vectors above. We can subset by supplying index for each dimension separated by a comma. So it looks like this:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a>a <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">9</span>, <span class="at">nrow=</span><span class="dv">3</span>)</span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(a) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"A"</span>, <span class="st">"B"</span>, <span class="st">"C"</span>)</span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a>a</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> A B C
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9</code></pre>
</div>
</div>
<p>Here is our matrix. Its similar to the one we used above except I named each column using the <code>colnames()</code> function.</p>
<p>So to subset this matrix and get just the first two rows I can do:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>a[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>,]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> A B C
[1,] 1 4 7
[2,] 2 5 8</code></pre>
</div>
</div>
<p>If I want to get the first two columns, I can do:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a>a[,<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> A B
[1,] 1 4
[2,] 2 5
[3,] 3 6</code></pre>
</div>
</div>
<p>I could also exclude a column:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>a[,<span class="sc">-</span><span class="dv">2</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> A C
[1,] 1 7
[2,] 2 8
[3,] 3 9</code></pre>
</div>
</div>
<p>Or get just one single value from the matrix:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb36"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a>a[<span class="dv">2</span>,<span class="dv">3</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>C
8 </code></pre>
</div>
</div>
<p>These skills are important because dataframes have the characteristics of both lists and matricies.</p>
<p>So lets load the Gay guides data from our class pacakge.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb38"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(DigitalMethodsData)</span>
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(<span class="st">"gayguides"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>To subset this data we could pull two particular columns like this:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb39"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="co">#gayguides[,2:3]</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>That returns columns 2 and 3 (including every single row!).</p>
<p>If we use two indices it’ll behave more like a matrix.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb40"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a>gayguides[<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>,]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> X ID title description streetaddress type
1 1 3213 'B.A.' (woods & ponds) Cruising Areas
2 2 2265 'B.A.' Beach 2 mi. E. Rte. 2 Cruising Areas
3 3 3269 'B.A.' Beach nr. Salt Air Beach Cruising Areas
amenityfeatures city state Year notes lat lon
1 Cruisy Area Lake Placid NY 1982 44.27949 -73.97987
2 Cruisy Area Troy NY 1981 42.72841 -73.69178
3 Cruisy Area Salt Lake City UT 1981 40.74781 -112.18727
status
1 Location could not be verified. General city or location coordinates used.
2 Location could not be verified. General city or location coordinates used.
3 Location could not be verified. General city or location coordinates used.</code></pre>
</div>
</div>
<ol start="7" class="example" type="1">
<li>Why is this different? What do we get compared to the previous example?</li>
</ol>
<blockquote class="blockquote">
<p>Subsetting a matrix is accomplished by the argument [rows,columns]. So, to return only columns you leave rows empty and return the desired column or range of columns. Same for rows.</p>
</blockquote>
<blockquote class="blockquote">
<p>Compared to the matrix above that was created, the gayguides dataset when queried like a matrix, returns formatting and information regarding the dataset (ie: how many rows are being displayed, how many of the total columns are being displayed)</p>
</blockquote>
<ol start="8" class="example" type="1">
<li>Can you find just the city and state for the first 5 entries?</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb42"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a>gayguides[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>,<span class="dv">8</span><span class="sc">:</span><span class="dv">9</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> city state
1 Lake Placid NY
2 Troy NY
3 Salt Lake City UT
4 Seattle WA
5 Troy NY</code></pre>
</div>
</div>
<ol start="9" class="example" type="1">
<li>How about the street address and type for rows 2,555 to 2,560?</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb44"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a>gayguides[<span class="dv">2555</span><span class="sc">:</span><span class="dv">2560</span>,<span class="dv">5</span><span class="sc">:</span><span class="dv">6</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> streetaddress type
2555 721 Lincoln Ave. Restaurant,Bars/Clubs
2556 3231 N. Clark St. Bars/Clubs
2557 721 Lincoln Ave. Bars/Clubs,Restaurant
2558 3231 N. Clark St. Bars/Clubs
2559 721 Lincoln Ave. Bars/Clubs,Restaurant
2560 4427 W. Kennedy Bars/Clubs</code></pre>
</div>
</div>
<ol start="10" class="example" type="1">
<li>Load another dataset from <code>DigitalMethodsData</code>. Subset it to find some element of the data. Write the code below and then explain what element of the data you wanted to subset and why:</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb46"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(<span class="st">"statepopulations"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb47"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a>statepopulations[<span class="dv">67</span>,<span class="dv">5</span><span class="sc">:</span><span class="dv">12</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> X1790 X1800 X1810 X1820 X1830 X1840 X1850 X1860
67 249073 345591 415115 502741 581185 594398 668507 703708</code></pre>
</div>
</div>
<blockquote class="blockquote">
<p>I subset the statepoplations dataset to get the growth of the population in South Carolina from the first census in 1790 to the year before the Civil War in 1860. This give me to data to compare to the growth of the backcountry of South Carolina.</p>
</blockquote>
<p>Another useful operator to know is <code>$</code> which we used a little bit in the previous worksheet. <code>$</code> is shorthand for accessing a variable in a dataframe.</p>
<p>So for example both of these produce the same result. One is just easier to type. Because the output here is so large, I’ve commented these two lines out. Uncomment them and the run the block.</p>
<div class="cell" data-r.options="{"max.print":10}">
<div class="sourceCode cell-code" id="cb49"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="co">#gayguides["title"]</span></span>
<span id="cb49-2"><a href="#cb49-2" aria-hidden="true" tabindex="-1"></a><span class="co">#gayguides$title</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<blockquote class="blockquote">
<p>Noticed when running the previous 2 commands in console for statepopulations that [“”] results in a single column output where as $ is formatted more like a matrix (rows and columns but without any real correlation to the formatting of the dataset)</p>
</blockquote>
<p>Notice the above output is <em>huge</em>. We can combine <code>$</code> and <code>[</code> to drill down more specifically like we did earlier.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb50"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a>gayguides<span class="sc">$</span>city[<span class="dv">100</span><span class="sc">:</span><span class="dv">110</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "San Diego" "Laguna Beach" "San Diego" "Sioux Falls"
[5] "Buffalo" "Long Beach" "Tucson" "Oceanside"
[9] "Santa Barbara" "Buffalo" "Washington" </code></pre>
</div>
</div>
<ol start="11" class="example" type="1">
<li>What does this code do?</li>
</ol>
<blockquote class="blockquote">
<p>Returns the values in the city column between rows 100 and 110</p>
</blockquote>
<p>We can also use <code>$</code> to create a new column in <code>gayguides</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb52"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a>gayguides<span class="sc">$</span>mynewcolumn <span class="ot"><-</span> <span class="cn">TRUE</span></span>
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a>gayguides<span class="sc">$</span>mynewcolumn[<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE</code></pre>
</div>
</div>
<p>What does this code do? It creates a new column in the gayguides data called <code>mynewcolumn</code> and it assigns <code>TRUE</code> to every row (all 60k). This is a bit of a unrealistic example. You probably won’t want to add the same value to 60k+ rows but the concept here is useful and we’ll use it later in this worksheet.</p>
</section>
</section>
</section>
<section id="control-structures-loops-choices" class="level3">
<h3 class="anchored" data-anchor-id="control-structures-loops-choices">Control Structures: Loops & Choices</h3>
<p>Loops and control structures can be indispensable tools when writing a script that does something for you. For example, maybe you want to write a script that goes out to a website, grabs a primary source, downloads it, and saves it to a file then repeats that hundreds of times until you have a set of primary sources. To write that kind of script you’d need loops and control structures to tell your program when to start, stop, and move to the next source.</p>
<p>Loops and control structures are one of those programming patterns that appear in almost every language. The syntax and style change, but the core idea doesn’t. This is a concept I’d recommend you master and it’ll come in handy for years to come.</p>
<p><strong>Choices</strong> and <strong>loops</strong> are both known as types of <strong>control structures.</strong></p>
<p>Choices, like <code>if</code>, allow you to run different code depending on the input.</p>
<p>Loops, like <code>for</code> and <code>while</code> allow you to repeatedly run code, typically with changing options.</p>
<p>Lets start with choices.</p>
<section id="choices" class="level4">
<h4 class="anchored" data-anchor-id="choices">Choices</h4>
<p>The most important choice statement in R is an <code>if</code> statement.</p>
<p>An <code>if</code> statement allows you to check the condition of something and then determine what should be done based on that input.</p>
<p>The basic format is this:</p>
<pre><code>if (condition) true_action
#OR
if(condition) true_action else false action </code></pre>
<p>And it works like this</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb55"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="dv">2</span> <span class="sc"><</span> <span class="dv">3</span>) <span class="fu">print</span>(<span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb57"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="dv">3</span> <span class="sc"><</span> <span class="dv">2</span>) <span class="fu">print</span>(<span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>The first example above works. Why? Because 2 is indeed less than 3. The second line has no output. Why? Becuase the conditions weren’t met. 3 is not less than 2 and so it didn’t print anything out. But we could ask it to do something else instead:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb58"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="dv">3</span> <span class="sc"><</span> <span class="dv">2</span>) <span class="fu">print</span>(<span class="cn">TRUE</span>) <span class="cf">else</span> <span class="fu">print</span>(<span class="st">"This is clearly false."</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "This is clearly false."</code></pre>
</div>
</div>
<p>Most often, if statements are more complex (known as compound statements) and so they use brackets like this:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb60"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> <span class="dv">71</span></span>
<span id="cb60-2"><a href="#cb60-2" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (x <span class="sc">></span> <span class="dv">90</span>) {</span>
<span id="cb60-3"><a href="#cb60-3" aria-hidden="true" tabindex="-1"></a> <span class="st">"A"</span></span>
<span id="cb60-4"><a href="#cb60-4" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> <span class="cf">if</span> (x <span class="sc">></span> <span class="dv">50</span>) {</span>
<span id="cb60-5"><a href="#cb60-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"C"</span></span>
<span id="cb60-6"><a href="#cb60-6" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb60-7"><a href="#cb60-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"F"</span></span>
<span id="cb60-8"><a href="#cb60-8" aria-hidden="true" tabindex="-1"></a> }</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "C"</code></pre>
</div>
</div>
<ol start="12" class="example" type="1">
<li>You are teaching History 100 for Clemson. Write an <code>if</code> statement that calculates whether the enrollment cap of 30 on your class has been met.</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb62"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a>e <span class="ot"><-</span> <span class="dv">31</span></span>
<span id="cb62-2"><a href="#cb62-2" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (e <span class="sc">></span> <span class="dv">30</span>){</span>
<span id="cb62-3"><a href="#cb62-3" aria-hidden="true" tabindex="-1"></a> <span class="st">"Full"</span></span>
<span id="cb62-4"><a href="#cb62-4" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> <span class="cf">if</span> (e <span class="sc"><</span> <span class="dv">30</span>){</span>
<span id="cb62-5"><a href="#cb62-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"Not Full"</span></span>
<span id="cb62-6"><a href="#cb62-6" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Full"</code></pre>
</div>
</div>
<ol start="13" class="example" type="1">
<li>Create a list of presidents and store it in a variable. Use an <code>if</code> statement to check if the number of presidents in the list is more than 5. If its not indicate that too.</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb64"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a>presidents <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Van Buren"</span>, <span class="st">"W. H. Harrison"</span>, <span class="st">"Tyler"</span>, <span class="st">"Polk"</span>, <span class="st">"Z. Taylor"</span>, <span class="st">"Fillmore"</span>, <span class="st">"Pierce"</span>, <span class="st">"Buchanan"</span>)</span>
<span id="cb64-2"><a href="#cb64-2" aria-hidden="true" tabindex="-1"></a><span class="fu">length</span>(presidents)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 8</code></pre>
</div>
<div class="sourceCode cell-code" id="cb66"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb66-1"><a href="#cb66-1" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">length</span>(presidents) <span class="sc">></span> <span class="dv">5</span>){</span>
<span id="cb66-2"><a href="#cb66-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span> (<span class="st">"Lots of presidents"</span>)</span>
<span id="cb66-3"><a href="#cb66-3" aria-hidden="true" tabindex="-1"></a> }<span class="cf">else</span> {</span>
<span id="cb66-4"><a href="#cb66-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span> (<span class="st">"Just a few presidents"</span>)</span>
<span id="cb66-5"><a href="#cb66-5" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Lots of presidents"</code></pre>
</div>
</div>
<ol start="14" class="example" type="1">
<li>You can also use an if statement to check a variable in a dataframe. How might you use an if statement to check if the GayGuides dataset contains any year after 1990? (Hint: first try to figure out how to determine the latest year in the dataframe and then build an if statement that checks that and prints something out if its true or false. You should think about what kind of value in contained in the Year column, how to access it, and how to check for the latest value.)</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb68"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb68-1"><a href="#cb68-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">max</span>(gayguides<span class="sc">$</span>Year)<span class="sc">></span><span class="dv">1990</span>){</span>
<span id="cb68-2"><a href="#cb68-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span> (<span class="st">"Contains years after 1990"</span>)</span>
<span id="cb68-3"><a href="#cb68-3" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span>{</span>
<span id="cb68-4"><a href="#cb68-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span> (<span class="st">"No years after 1990"</span>)</span>
<span id="cb68-5"><a href="#cb68-5" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "No years after 1990"</code></pre>
</div>
</div>
<ol start="15" class="example" type="1">
<li>Reflect on the question above. How did you figure this out? What was the process you went through to build this chunk of code?</li>
</ol>
<blockquote class="blockquote">
<p>I googled how to return the largest number in list and used the max() function on the subset of Year in the gayguides dataset. I tested that and it returned the value 1985. I then built my if else statement to return the text when run.</p>
</blockquote>
</section>
<section id="loops" class="level4">
<h4 class="anchored" data-anchor-id="loops">Loops</h4>
<p>A **loop* is used to iterate over items in a vector. They typically follow this form:</p>
<pre><code>for (item in vector) perform_action</code></pre>
<p>For each item in the vector, perform_action is called once and the value of the item is updated each time.</p>
<p>For example,</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb71"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb71-1"><a href="#cb71-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>) {</span>
<span id="cb71-2"><a href="#cb71-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span>(i)</span>
<span id="cb71-3"><a href="#cb71-3" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1
[1] 2
[1] 3</code></pre>
</div>
</div>
<p>What does this loop do? <code>1:3</code> is shorthand for 1, 2, 3. Run it in your terminal <em>console</em> to see its output. This code says, for every number in the range 1:3, print the number.</p>
<p>We could do this with character vectors too.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb73"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb73-1"><a href="#cb73-1" aria-hidden="true" tabindex="-1"></a>presidents <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"George Washington"</span>, <span class="st">"John Adams"</span>, <span class="st">"Thomas Jefferson"</span>)</span>
<span id="cb73-2"><a href="#cb73-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (p <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(presidents)) {</span>
<span id="cb73-3"><a href="#cb73-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span>(presidents[p])</span>
<span id="cb73-4"><a href="#cb73-4" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "George Washington"
[1] "John Adams"
[1] "Thomas Jefferson"</code></pre>
</div>
</div>
<ol start="16" class="example" type="1">
<li>Why does this work? What does <code>length()</code> do and why do we use it on <code>presidents</code>?</li>
</ol>
<blockquote class="blockquote">
<p>length() counts the number of items in a list or vector. It converts character strings into a variable count (in other words, it allows the character strings to be read as a number in a list). So the code says to take every item (p) in the list president and to print the corresponding text in each item’s corresponding spot in the list.</p>
</blockquote>
<p>Create a character vector that contains the title of each of your classes this semester. Can you write a <code>for</code> loop that prints out “I am enrolled in” and the name of each of your classes? (“I am enrolled in History 8150”, “I am enrolled in History 8000”…..etc). Hint: you’ll need a function to combine the output and some text of your choosing inside the function.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb75"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb75-1"><a href="#cb75-1" aria-hidden="true" tabindex="-1"></a>ClassesSpring2024 <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Digital Methods II"</span>, <span class="st">"Southern Seminar"</span>, <span class="st">"Thesis Hours"</span>)</span>
<span id="cb75-2"><a href="#cb75-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (c <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(ClassesSpring2024))</span>
<span id="cb75-3"><a href="#cb75-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span> (<span class="fu">paste</span>(<span class="st">"I am enrolled in"</span>, ClassesSpring2024[c]))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "I am enrolled in Digital Methods II"
[1] "I am enrolled in Southern Seminar"
[1] "I am enrolled in Thesis Hours"</code></pre>
</div>
</div>
<p>Sometimes we want our loop to terminate itself early. There are two statements that help us do that:</p>
<ul>
<li><code>next</code> exits the current iteration</li>
<li><code>break</code> exits the entire for loop</li>
</ul>
<div class="cell">
<div class="sourceCode cell-code" id="cb77"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb77-1"><a href="#cb77-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>) {</span>
<span id="cb77-2"><a href="#cb77-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (i <span class="sc"><</span> <span class="dv">3</span>)</span>
<span id="cb77-3"><a href="#cb77-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">next</span></span>
<span id="cb77-4"><a href="#cb77-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb77-5"><a href="#cb77-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span> (i)</span>
<span id="cb77-6"><a href="#cb77-6" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10</code></pre>
</div>
</div>
<p><code>Next</code> skips values until the criteria is met. So in the above example it skips numbers 1 and 2 because they don’t match the criteria. But in some cases we may not want to skip the entries but rather exit the loop entirely. Something like this:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb79"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb79-1"><a href="#cb79-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>) {</span>
<span id="cb79-2"><a href="#cb79-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (i <span class="sc"><</span> <span class="dv">3</span>)</span>
<span id="cb79-3"><a href="#cb79-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">next</span></span>
<span id="cb79-4"><a href="#cb79-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb79-5"><a href="#cb79-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (i <span class="sc">>=</span> <span class="dv">5</span>)</span>
<span id="cb79-6"><a href="#cb79-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">break</span></span>
<span id="cb79-7"><a href="#cb79-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb79-8"><a href="#cb79-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span> (i)</span>
<span id="cb79-9"><a href="#cb79-9" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3
[1] 4</code></pre>
</div>
</div>
<ol start="17" class="example" type="1">
<li>What happened here? Why did the loop only print 3 and 4?</li>
</ol>
<blockquote class="blockquote">
<p>For a list of numbers 1 thru 10 if any item is less than 3 next causes it to skip those items and if any item is equal to or greater than 5 it causes a break and stops the loop. Therefore, the items (numbers) stored and printed as i becomes 3 and 4.</p>
</blockquote>
<ol start="18" class="example" type="1">
<li>In the state population data, can you write a loop that pulls only states with populations between 200,000 and 250,000 in 1800? You could accomplish this a few different ways. Try to use next and break statements.</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb81"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb81-1"><a href="#cb81-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(statepopulations<span class="sc">$</span>X1800)){</span>
<span id="cb81-2"><a href="#cb81-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb81-3"><a href="#cb81-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (i <span class="sc">==</span> <span class="st">"NA"</span>){</span>
<span id="cb81-4"><a href="#cb81-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">next</span></span>
<span id="cb81-5"><a href="#cb81-5" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb81-6"><a href="#cb81-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span> <span class="cf">if</span> (i <span class="sc"><</span> <span class="dv">200000</span>){</span>
<span id="cb81-7"><a href="#cb81-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">next</span></span>
<span id="cb81-8"><a href="#cb81-8" aria-hidden="true" tabindex="-1"></a> } </span>
<span id="cb81-9"><a href="#cb81-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span> <span class="cf">if</span> (i <span class="sc">></span> <span class="dv">250000</span>){</span>
<span id="cb81-10"><a href="#cb81-10" aria-hidden="true" tabindex="-1"></a> <span class="cf">next</span></span>
<span id="cb81-11"><a href="#cb81-11" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb81-12"><a href="#cb81-12" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb81-13"><a href="#cb81-13" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb81-14"><a href="#cb81-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span>(statepopulations<span class="sc">$</span>STATE[i])</span>
<span id="cb81-15"><a href="#cb81-15" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb82"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb82-1"><a href="#cb82-1" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(<span class="st">"statepopulations"</span>)</span>
<span id="cb82-2"><a href="#cb82-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb82-3"><a href="#cb82-3" aria-hidden="true" tabindex="-1"></a>statesWithPopBetween200000And250000In1800 <span class="ot"><-</span> <span class="fu">subset</span>(statepopulations, X1800 <span class="sc">></span> <span class="dv">200000</span> <span class="sc">&</span> X1800 <span class="sc"><</span> <span class="dv">250000</span>, <span class="at">select =</span> <span class="fu">c</span>(STATE, X1800), <span class="at">drop =</span> <span class="cn">FALSE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ol start="19" class="example" type="1">
<li>What if we wanted to loop through the gay guides data? Can you write a loop that iterates through the cities in the Gay Guides data and returns the title of any location in Greenville?</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb83"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb83-1"><a href="#cb83-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(gayguides<span class="sc">$</span>city)){</span>
<span id="cb83-2"><a href="#cb83-2" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb83-3"><a href="#cb83-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(gayguides<span class="sc">$</span>city[i] <span class="sc">==</span> <span class="st">"Greenville"</span>)</span>
<span id="cb83-4"><a href="#cb83-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span> (gayguides<span class="sc">$</span>title[i])</span>
<span id="cb83-5"><a href="#cb83-5" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "'Rest Stop'"
[1] "'Rest Stop'"
[1] "'Rest Stop'"
[1] "'Rest Stop'"
[1] "'Rest Stop' on I-85 S."
[1] "Club Gemini"
[1] "Club Gemini"
[1] "Club Gemini"
[1] "Club Gemini"
[1] "Club Gemini"
[1] "Greyhound Bus Depot"
[1] "Greyhound Bus Depot"
[1] "Greyhound Bus Depot"
[1] "Greyhound Bus Depot"
[1] "Greyhound Bus Dept"
[1] "Hiltop Adult Bookstore"
[1] "M.C.C."
[1] "M.C.C."
[1] "M.C.C."
[1] "M.C.C."
[1] "Paddock Club"
[1] "Paddock Club"
[1] "Paddock Club"
[1] "Paddock Club"
[1] "Paddock Club"
[1] "Parking lot"
[1] "Parking lot"
[1] "Parking lot"
[1] "Parking lot opp. Methodist Church"
[1] "Parking lot opp. Methodist Church"
[1] "Reedy River Park"
[1] "Sheraton Motor Inn"
[1] "Sheraton Motor Inn"
[1] "Sheraton Motor Inn"
[1] "Sheraton Motor Inn"
[1] "Sheraton Motor Inn"
[1] "Stone Castle"
[1] "Stone Castle"
[1] "Stone Castle"
[1] "Stone Castle"
[1] "Stone Castle"
[1] "Trailways Bus Depot"
[1] "Trailways Bus Depot"
[1] "Trailways Bus Depot"
[1] "Trailways Bus Depot"
[1] "Trailways Bus Depot"
[1] "'Rest Stop' on I-85 N"
[1] "Rest Stop on I-85 N. betw. Greenville & Anderson, GA"
[1] "Club Gemini"
[1] "Club Gemini"
[1] "Club Gemini "
[1] "Disco Lounge "
[1] "Executive Sportsman Club"
[1] "Executive Sportsman Club "
[1] "Fiddlers Three"
[1] "Garden Lounge"
[1] "Garden Lounge"
[1] "Greyhound Bus Depot"
[1] "Greyhound Bus Depot"
[1] "Greyhound Bus Depot & parking lot across from Methodist Church"
[1] "Horseshoe Lounge"
[1] "Horseshoe Lounge"
[1] "Horseshoe Lounge"
[1] "Paddock Club"
[1] "Paddock Club"
[1] "Paddock Club "
[1] "Paddock Club "
[1] "Parking lot opp. Methodist Church"
[1] "Parking lot opposite Methodist Church"
[1] "Patio"
[1] "Patio"
[1] "Patio"
[1] "Patio"
[1] "R & H Lounge"
[1] "R&H Lounge"
[1] "Rose's Dept. Store T-Room"
[1] "Shearton Motor Inn"
[1] "Smith's Hotel"
[1] "Smith's Motel "
[1] "Trailways Bus Depot"
[1] "Trinking Club"</code></pre>
</div>
</div>
<p><code>while</code> is another useful tool for writing loops. While will run a loop <em>while</em> a condition is true. Once that condition is not true, it exits. For example:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb85"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb85-1"><a href="#cb85-1" aria-hidden="true" tabindex="-1"></a>i <span class="ot"><-</span> <span class="dv">1</span></span>
<span id="cb85-2"><a href="#cb85-2" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> (i <span class="sc"><</span> <span class="dv">6</span>) {</span>
<span id="cb85-3"><a href="#cb85-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span>(i)</span>
<span id="cb85-4"><a href="#cb85-4" aria-hidden="true" tabindex="-1"></a> i <span class="ot"><-</span> i<span class="sc">+</span><span class="dv">1</span></span>
<span id="cb85-5"><a href="#cb85-5" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1
[1] 2
[1] 3
[1] 4
[1] 5</code></pre>
</div>
</div>
<p>Its important to note here that <code>i</code> is defined outside the loop. Its initial value is 1 but each time the loop runs it gets 1 added to it. So the first time it runs, i = 1 and the loop prints the value then adds 1 to i. Now i = 2 but since that is still less than 6 the loop continues and prints the value then adds 1 to i. Eventually, i=6 and since 6 is not less than 6 the loop exits without printing.</p>
<p><code>While</code> doesn’t get used as often in R because there is often a more efficient way to do the same thing. But in other languages <code>while</code> gets used much more often. Its a good pattern to be familiar with even if you don’t use in frequently in R.</p>
</section>
</section>
</section>
<section id="functions" class="level2">
<h2 class="anchored" data-anchor-id="functions">Functions</h2>
<p>A <strong>function</strong> is a set of statements that are organized together to perform a specific task. You’ve already been using functions because R has a number of built in functions that are available to users. For example. <code>print()</code> is a function.</p>
<p>To be more specific, a <strong>function</strong> is a set of code that performs a task and returns a result. As you advance your programming skills you’ll probably have certain tasks that you perform frequently. Once you’ve run a chunk of code several times its good practice to turn it into a function so it can be repeatedly used.</p>
<p>A function in R takes any arguments that may be necessary to perform the actions contained within the function, performs those functions, and then returns any result in the console.</p>
<p>For example, the below function takes a number as an input, multiplies it by 5 and then returns the result.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb87"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb87-1"><a href="#cb87-1" aria-hidden="true" tabindex="-1"></a>myfunction <span class="ot"><-</span> <span class="cf">function</span>(y){</span>
<span id="cb87-2"><a href="#cb87-2" aria-hidden="true" tabindex="-1"></a> myvalue <span class="ot"><-</span> y <span class="sc">*</span> <span class="dv">5</span></span>
<span id="cb87-3"><a href="#cb87-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span>(myvalue)</span>
<span id="cb87-4"><a href="#cb87-4" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>When you run this nothing is returned. Why? You’ve loaded the function but you haven’t called it yet. We can do that like this:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb88"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb88-1"><a href="#cb88-1" aria-hidden="true" tabindex="-1"></a><span class="fu">myfunction</span>(<span class="dv">5</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 25</code></pre>
</div>
</div>
<p>You’ll notice that the variable we created inside the function, <code>myvalue</code>, doesn’t show up. Unless we write code asking R to return the value of that variable, it’ll run invisibly in the background and only give us back the result we ask for. This comes in handy when you are writing more complex functions and need to store bits of data temporarily for a calculation that is being run by the function.</p>
<p>Here’s another example:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb90"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb90-1"><a href="#cb90-1" aria-hidden="true" tabindex="-1"></a>historyStudents <span class="ot"><-</span> <span class="cf">function</span>(studentname, class) {</span>
<span id="cb90-2"><a href="#cb90-2" aria-hidden="true" tabindex="-1"></a> statement <span class="ot"><-</span> <span class="fu">paste</span>(<span class="st">"Hello"</span>, studentname, <span class="st">". Welcome to "</span>, class, <span class="st">"!"</span>, <span class="at">sept=</span><span class="st">""</span>)</span>
<span id="cb90-3"><a href="#cb90-3" aria-hidden="true" tabindex="-1"></a> statement</span>
<span id="cb90-4"><a href="#cb90-4" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ol start="20" class="example" type="1">
<li>Can you run this function?</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb91"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb91-1"><a href="#cb91-1" aria-hidden="true" tabindex="-1"></a><span class="fu">historyStudents</span>(<span class="st">"Hilda Rae"</span>, <span class="st">"US History 101"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Hello Hilda Rae . Welcome to US History 101 ! "</code></pre>
</div>
</div>
<p>There are several components to a function.</p>
<ul>
<li><strong>Function Name</strong>:This is the actual name of the function. It is stored in R environment as an object with this name. In the above example, the function name is <code>historyStudents</code></li>
<li><strong>Arguments</strong>: An argument is a placeholder. When a function is invoked, you pass a value to the argument. Arguments are optional; that is, a function may contain no arguments. Also arguments can have default values. In the example above, the function takes two arguments: a student’s name and the name of a class.</li>
<li><strong>Function Body</strong>: The function body contains a collection of statements that defines what the function does. In the above example the function body is the code inside the <code>{</code> and <code>}</code> brackets.</li>
<li><strong>Return Value</strong>: The return value of a function is the last expression in the function body to be evaluated. In the above example, our return value is <code>statement</code> which prints our welcome statement using the names we provided in the arguments.</li>
</ul>
<ol start="21" class="example" type="1">
<li>Write a function that takes a string of text and counts the number of characters. The function should return “There are xx characters in that string.”</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb93"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb93-1"><a href="#cb93-1" aria-hidden="true" tabindex="-1"></a>countTheLetters <span class="ot"><-</span> <span class="cf">function</span>(w){</span>
<span id="cb93-2"><a href="#cb93-2" aria-hidden="true" tabindex="-1"></a> result <span class="ot"><-</span> <span class="fu">paste</span> (<span class="st">"There are"</span>, <span class="fu">nchar</span>(w), <span class="st">"characters in that string."</span>)</span>
<span id="cb93-3"><a href="#cb93-3" aria-hidden="true" tabindex="-1"></a> result</span>
<span id="cb93-4"><a href="#cb93-4" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb93-5"><a href="#cb93-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb93-6"><a href="#cb93-6" aria-hidden="true" tabindex="-1"></a><span class="fu">countTheLetters</span>(<span class="st">"Savannah"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "There are 8 characters in that string."</code></pre>
</div>
</div>
<ol start="22" class="example" type="1">
<li>Reflect on the process of building the above function. How did you go about figuring this out?</li>
</ol>
<blockquote class="blockquote">
<p>I used a combination of the previous 2 examples to create the function. I googled how to count the number of characters in a string and discovered R has a built in function (nchar) which would return the character count which I could then nest into the required statement.</p>
</blockquote>
<p>The body of a function can use any of the techniques we’ve learned in this worksheet like loops and if statements. For example:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb95"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb95-1"><a href="#cb95-1" aria-hidden="true" tabindex="-1"></a>grade <span class="ot"><-</span> <span class="cf">function</span>(x) {</span>
<span id="cb95-2"><a href="#cb95-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (x <span class="sc">></span> <span class="dv">90</span>) {</span>
<span id="cb95-3"><a href="#cb95-3" aria-hidden="true" tabindex="-1"></a> <span class="st">"A"</span></span>
<span id="cb95-4"><a href="#cb95-4" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> <span class="cf">if</span> (x <span class="sc">></span> <span class="dv">50</span>) {</span>
<span id="cb95-5"><a href="#cb95-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"C"</span></span>
<span id="cb95-6"><a href="#cb95-6" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb95-7"><a href="#cb95-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"F"</span></span>
<span id="cb95-8"><a href="#cb95-8" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb95-9"><a href="#cb95-9" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb95-10"><a href="#cb95-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb95-11"><a href="#cb95-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb95-12"><a href="#cb95-12" aria-hidden="true" tabindex="-1"></a><span class="fu">grade</span>(<span class="dv">85</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "C"</code></pre>
</div>
</div>
<p>We could run this function as many times as we want.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb97"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb97-1"><a href="#cb97-1" aria-hidden="true" tabindex="-1"></a><span class="fu">grade</span>(<span class="dv">95</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "A"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb99"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb99-1"><a href="#cb99-1" aria-hidden="true" tabindex="-1"></a><span class="fu">grade</span>(<span class="dv">75</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "C"</code></pre>
</div>
</div>
<ol start="23" class="example" type="1">
<li>In the example below, why doesn’t George Washington’s grade get printed out?</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb101"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb101-1"><a href="#cb101-1" aria-hidden="true" tabindex="-1"></a>GeorgeWashington <span class="ot"><-</span> <span class="fu">grade</span>(<span class="dv">60</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<blockquote class="blockquote">
<p>Because you’ve attempted to assign a function to a variable. Only strings, integers, or logical values can be assigned to variables. I think you would have to use getFunction (somehow?) to incorporate a function into a variable.</p>
</blockquote>
<p>Here’s a more complex example:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb102"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb102-1"><a href="#cb102-1" aria-hidden="true" tabindex="-1"></a>gg.locations <span class="ot"><-</span> <span class="cf">function</span> (city, year) {</span>
<span id="cb102-2"><a href="#cb102-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">data</span>(gayguides)</span>
<span id="cb102-3"><a href="#cb102-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb102-4"><a href="#cb102-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(gayguides<span class="sc">$</span>city)) {</span>
<span id="cb102-5"><a href="#cb102-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (gayguides<span class="sc">$</span>city[i] <span class="sc">==</span> city <span class="sc">&&</span> gayguides<span class="sc">$</span>Year[i] <span class="sc">==</span> year) {</span>
<span id="cb102-6"><a href="#cb102-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span>(<span class="fu">paste</span>(<span class="st">"Found a location called "</span>, gayguides<span class="sc">$</span>title[i]))</span>
<span id="cb102-7"><a href="#cb102-7" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb102-8"><a href="#cb102-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">next</span> </span>
<span id="cb102-9"><a href="#cb102-9" aria-hidden="true" tabindex="-1"></a> } </span>
<span id="cb102-10"><a href="#cb102-10" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb102-11"><a href="#cb102-11" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb102-12"><a href="#cb102-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb102-13"><a href="#cb102-13" aria-hidden="true" tabindex="-1"></a><span class="fu">gg.locations</span>(<span class="st">"Greenville"</span>, <span class="dv">1980</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Found a location called 'Rest Stop' on I-85 N"
[1] "Found a location called Club Gemini"
[1] "Found a location called Executive Sportsman Club"
[1] "Found a location called Greyhound Bus Depot"
[1] "Found a location called Paddock Club"
[1] "Found a location called Parking lot opposite Methodist Church"
[1] "Found a location called Shearton Motor Inn"
[1] "Found a location called Trailways Bus Depot"</code></pre>
</div>
</div>
<ol start="24" class="example" type="1">
<li>Write a function that uses the things you’ve learned in this worksheet. The function should accept two arguments, city and year. It should pull all the locations in that city and year and return a statement that says “Found a location called xxx”. (Where x is the title of a location in that year and city.)</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb104"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb104-1"><a href="#cb104-1" aria-hidden="true" tabindex="-1"></a>populationIn1800 <span class="ot"><-</span> <span class="cf">function</span> (state) {</span>
<span id="cb104-2"><a href="#cb104-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">data</span> (<span class="st">"statepopulations"</span>)</span>
<span id="cb104-3"><a href="#cb104-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb104-4"><a href="#cb104-4" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(statepopulations<span class="sc">$</span>STATE)) {</span>
<span id="cb104-5"><a href="#cb104-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (statepopulations<span class="sc">$</span>STATE[i] <span class="sc">==</span> state) {</span>
<span id="cb104-6"><a href="#cb104-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span> (<span class="fu">paste</span> (<span class="st">"The population in"</span>, state, <span class="st">"in 1800 was"</span>, statepopulations<span class="sc">$</span>X1800))</span>
<span id="cb104-7"><a href="#cb104-7" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb104-8"><a href="#cb104-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">next</span></span>
<span id="cb104-9"><a href="#cb104-9" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb104-10"><a href="#cb104-10" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb104-11"><a href="#cb104-11" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb104-12"><a href="#cb104-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb104-13"><a href="#cb104-13" aria-hidden="true" tabindex="-1"></a><span class="fu">populationIn1800</span>(<span class="st">"New Hampshire"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "The population in New Hampshire in 1800 was NA"
[2] "The population in New Hampshire in 1800 was NA"
[3] "The population in New Hampshire in 1800 was NA"
[4] "The population in New Hampshire in 1800 was NA"
[5] "The population in New Hampshire in 1800 was NA"
[6] "The population in New Hampshire in 1800 was NA"
[7] "The population in New Hampshire in 1800 was NA"
[8] "The population in New Hampshire in 1800 was NA"
[9] "The population in New Hampshire in 1800 was NA"
[10] "The population in New Hampshire in 1800 was NA"
[11] "The population in New Hampshire in 1800 was 251002"
[12] "The population in New Hampshire in 1800 was NA"
[13] "The population in New Hampshire in 1800 was 64273"
[14] "The population in New Hampshire in 1800 was 14093"
[15] "The population in New Hampshire in 1800 was NA"
[16] "The population in New Hampshire in 1800 was NA"
[17] "The population in New Hampshire in 1800 was 162686"
[18] "The population in New Hampshire in 1800 was NA"
[19] "The population in New Hampshire in 1800 was NA"
[20] "The population in New Hampshire in 1800 was NA"
[21] "The population in New Hampshire in 1800 was NA"
[22] "The population in New Hampshire in 1800 was NA"
[23] "The population in New Hampshire in 1800 was NA"
[24] "The population in New Hampshire in 1800 was NA"
[25] "The population in New Hampshire in 1800 was NA"
[26] "The population in New Hampshire in 1800 was 5641"
[27] "The population in New Hampshire in 1800 was NA"
[28] "The population in New Hampshire in 1800 was NA"
[29] "The population in New Hampshire in 1800 was NA"
[30] "The population in New Hampshire in 1800 was NA"
[31] "The population in New Hampshire in 1800 was 220955"
[32] "The population in New Hampshire in 1800 was NA"
[33] "The population in New Hampshire in 1800 was NA"
[34] "The population in New Hampshire in 1800 was NA"
[35] "The population in New Hampshire in 1800 was 341548"
[36] "The population in New Hampshire in 1800 was 574564"
[37] "The population in New Hampshire in 1800 was NA"
[38] "The population in New Hampshire in 1800 was NA"
[39] "The population in New Hampshire in 1800 was NA"
[40] "The population in New Hampshire in 1800 was NA"
[41] "The population in New Hampshire in 1800 was NA"
[42] "The population in New Hampshire in 1800 was 8850"
[43] "The population in New Hampshire in 1800 was NA"
[44] "The population in New Hampshire in 1800 was NA"
[45] "The population in New Hampshire in 1800 was NA"
[46] "The population in New Hampshire in 1800 was NA"
[47] "The population in New Hampshire in 1800 was NA"
[48] "The population in New Hampshire in 1800 was NA"
[49] "The population in New Hampshire in 1800 was NA"
[50] "The population in New Hampshire in 1800 was NA"
[51] "The population in New Hampshire in 1800 was 183858"
[52] "The population in New Hampshire in 1800 was 211149"
[53] "The population in New Hampshire in 1800 was NA"
[54] "The population in New Hampshire in 1800 was NA"
[55] "The population in New Hampshire in 1800 was 589051"
[56] "The population in New Hampshire in 1800 was 478103"
[57] "The population in New Hampshire in 1800 was NA"
[58] "The population in New Hampshire in 1800 was 45365"
[59] "The population in New Hampshire in 1800 was NA"
[60] "The population in New Hampshire in 1800 was NA"
[61] "The population in New Hampshire in 1800 was NA"
[62] "The population in New Hampshire in 1800 was NA"
[63] "The population in New Hampshire in 1800 was NA"
[64] "The population in New Hampshire in 1800 was NA"
[65] "The population in New Hampshire in 1800 was 602365"
[66] "The population in New Hampshire in 1800 was 69122"
[67] "The population in New Hampshire in 1800 was 345591"
[68] "The population in New Hampshire in 1800 was NA"
[69] "The population in New Hampshire in 1800 was NA"
[70] "The population in New Hampshire in 1800 was 105602"
[71] "The population in New Hampshire in 1800 was NA"
[72] "The population in New Hampshire in 1800 was NA"
[73] "The population in New Hampshire in 1800 was NA"
[74] "The population in New Hampshire in 1800 was 154465"
[75] "The population in New Hampshire in 1800 was 880200"
[76] "The population in New Hampshire in 1800 was NA"