-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-Day1_lost_cats.html
1435 lines (1303 loc) · 73.9 KB
/
01-Day1_lost_cats.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">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<title>R Camp | day 1</title>
<script src="01-Day1_lost_cats_files/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="01-Day1_lost_cats_files/bootstrap-3.3.5/css/readable.min.css" rel="stylesheet" />
<script src="01-Day1_lost_cats_files/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="01-Day1_lost_cats_files/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="01-Day1_lost_cats_files/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="01-Day1_lost_cats_files/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="01-Day1_lost_cats_files/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="01-Day1_lost_cats_files/tocify-1.9.1/jquery.tocify.js"></script>
<script src="01-Day1_lost_cats_files/navigation-1.1/tabsets.js"></script>
<link href="01-Day1_lost_cats_files/font-awesome-5.0.13/css/fa-svg-with-js.css" rel="stylesheet" />
<script src="01-Day1_lost_cats_files/font-awesome-5.0.13/js/fontawesome-all.min.js"></script>
<script src="01-Day1_lost_cats_files/font-awesome-5.0.13/js/fa-v4-shims.min.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; background-color: #f8f8f8; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
pre, code { background-color: #f8f8f8; }
code > span.kw { color: #204a87; font-weight: bold; } /* Keyword */
code > span.dt { color: #204a87; } /* DataType */
code > span.dv { color: #0000cf; } /* DecVal */
code > span.bn { color: #0000cf; } /* BaseN */
code > span.fl { color: #0000cf; } /* Float */
code > span.ch { color: #4e9a06; } /* Char */
code > span.st { color: #4e9a06; } /* String */
code > span.co { color: #8f5902; font-style: italic; } /* Comment */
code > span.ot { color: #8f5902; } /* Other */
code > span.al { color: #ef2929; } /* Alert */
code > span.fu { color: #000000; } /* Function */
code > span.er { color: #a40000; font-weight: bold; } /* Error */
code > span.wa { color: #8f5902; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #000000; } /* Constant */
code > span.sc { color: #000000; } /* SpecialChar */
code > span.vs { color: #4e9a06; } /* VerbatimString */
code > span.ss { color: #4e9a06; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #000000; } /* Variable */
code > span.cf { color: #204a87; font-weight: bold; } /* ControlFlow */
code > span.op { color: #ce5c00; font-weight: bold; } /* Operator */
code > span.pp { color: #8f5902; font-style: italic; } /* Preprocessor */
code > span.ex { } /* Extension */
code > span.at { color: #c4a000; } /* Attribute */
code > span.do { color: #8f5902; font-weight: bold; font-style: italic; } /* Documentation */
code > span.an { color: #8f5902; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #8f5902; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #8f5902; font-weight: bold; font-style: italic; } /* Information */
</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
<link rel="stylesheet" href="css\camp_style.css" type="text/css" />
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
</style>
<div class="container-fluid main-container">
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_').toLowerCase();
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
padding-left: 25px;
text-indent: 0;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">R Camp | day 1</h1>
</div>
<div id="good-morning-detective" class="section level3 unnumbered">
<h3>Good morning, Detective</h3>
<p>We’ve got a new case to crack. Let’s open RStudio.</p>
<p><br></p>
<p style="display:block; text-align:center;">
<img src="https://pbs.twimg.com/media/DNhps0gUQAAcS55.jpg" width="520">
</p>
<p><br><br></p>
</div>
<div id="welcome-to-r-camp" class="section level1 unnumbered">
<h1>Welcome to R Camp!</h1>
<hr>
<p><img src="http://placekitten.com/g/247/252" /> <img src="http://placekitten.com/g/249/251" /> <img src="http://placekitten.com/g/249/253" /></p>
<p><br></p>
<p>We are <strong>Kristie</strong>, <strong>Dorian</strong>, and <strong>Derek</strong> and we like <strong>R</strong>.</p>
<p>We are cats and not computer scientists.</p>
<p>We make lots of mistakes. You will see us make mistakes. Feel free to laugh at us. It’s okay.</p>
<p><br></p>
<div id="disclaimer" class="section level3 unnumbered">
<h3><i class="fa fa-exclamation-triangle" aria-hidden="true" style="color:orange;"></i> Disclaimer</h3>
<p>Spelling is very important in R.</p>
<p><br></p>
<div class="figure">
<img src="https://blog.wdtinc.com/hs-fs/hubfs/blog-files/lightning_spelling.jpg?t=1509031247746&width=600&name=lightning_spelling.jpg" />
</div>
<p><br></p>
</div>
</div>
<div id="new-project" class="section level1">
<h1><span class="header-section-number">1</span> | New project</h1>
<hr>
<p>Okay detective, let’s open a new dossier for today’s case.</p>
<p><br></p>
<p><strong>Start a new project</strong></p>
<ul>
<li>In <em>Rstudio</em> select <em>File</em> from the top menu bar</li>
<li>Choose <em>New Project…</em></li>
<li>Choose <em>New Directory</em></li>
<li>Choose <em>New Project</em></li>
<li>Enter a project name such as <code>"cat_mystery"</code></li>
<li>Select <em>Browse…</em> and choose a folder where you normally perform your work.</li>
<li>Click <em>Create Project</em></li>
</ul>
<div id="new-r-script" class="section level2">
<h2><span class="header-section-number">1.1</span> New R script</h2>
<p>R “scripts” are where you write your R code and document your work. They’re like recipes or prescriptions you write the tell the computer what you want to happen to your data.</p>
<p><strong>Open a new R script</strong></p>
<ul>
<li>In the upper left, click on the white file icon with the green (+) sign.</li>
<li>Select <em>R Script</em>.</li>
</ul>
<div class="figure">
<img src="http://docplayer.net/docs-images/25/5462802/images/10-0.png" width="450" />
</div>
<p><br></p>
<p><strong>Save script</strong></p>
<ul>
<li>Click on the floppy disk icon</li>
<li>Enter a file name such as <code>cat_code.R</code></li>
</ul>
</div>
<div id="a-tour-of-r-studio" class="section level2">
<h2><span class="header-section-number">1.2</span> A tour of R Studio</h2>
<p><img src="http://www.sthda.com/sthda/RDoc/images/rstudio.png" width="580"></p>
<p><br></p>
<p><strong>1. Code editor</strong></p>
<p>This is where you write your scripts and document your work. The tabs at the top of the code editor allow you to view scripts and data sets you have open. This is where you’ll spend most of your time.</p>
<p><strong>2. Console</strong></p>
<p>This is where code is actually executed by the computer. It shows code that you have run and any errors, warnings, or other messages resulting from that code. You can input code directly into the console and run it, but it won’t be saved for later. That’s why we like to run all of our code directly from a script in the code editor.</p>
<p><strong>3. Workspace</strong></p>
<p>This pane shows all of the objects and functions that you have created, as well as a history of the code you have run during your current session. The environment tab shows all of your objects and functions. The history tab shows the code you have run. Note the <em>broom</em> icon below the Connections tab. This cleans shop and allows you to clear all of the objects in your workspace.</p>
<p><strong>4. Plots and files</strong></p>
<p>These tabs allows you to view and open files in your current directory, view plots and other visual objects like maps, view your installed packages and their functions, and access the help window. If at anytime you’re unsure what a function or package does, enter the name of thing after a question mark. For example, try entering <code>?mean</code> into the console and push <strong>ENTER</strong>.</p>
</div>
<div id="make-it-your-own" class="section level2">
<h2><span class="header-section-number">1.3</span> Make it your own</h2>
<p>Let’s add a little style so we feel more at home. Follow these steps to change the font-size and and color scheme:</p>
<ol style="list-style-type: decimal">
<li>Go to <strong>Tools</strong> on the top navigation bar.</li>
<li>Choose <code>Global Options...</code></li>
<li>Choose <code>Appearance</code> with the paint bucket.</li>
<li>Find something you like.</li>
</ol>
<p><br></p>
<div class="figure">
<img src="images/font_change.png" style="margin-left:20px;" width="420" />
</div>
</div>
</div>
<div id="first-steps" class="section level1">
<h1><span class="header-section-number">2</span> | First steps</h1>
<hr>
<p>You can create objects and assign values to them using the “left arrow” <code><-</code>, more officially known as the assignment operator. Try adding the code below to your R script and creating an object called <code>name</code>.</p>
<p>Once you add the code to your script, you can run the code by moving the blinking cursor to that line and pressing <strong>CTRL + ENTER</strong>.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Create a new object</span>
cat <-<span class="st"> "Derek"</span>
cat
<span class="co"># When saving text to a character object you need quotation marks.</span>
<span class="co"># This won't work.</span>
cat <-<span class="st"> </span>Demitri
<span class="co"># Without quotes, R looks for an object called Derek, and then let you know that it couldn't find one. </span>
<span class="co"># You can copy an object by saving it to a new name.</span>
cat2 <-<span class="st"> </span>cat
<span class="co"># Overwrite an object</span>
cat <-<span class="st"> "Demitri III"</span>
cat
<span class="co"># Did name2 change as well?</span>
cat2 </code></pre></div>
<p><br></p>
<p>If you create a name you don’t like you can drop it with the function <code>rm()</code>.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Delete objects to clean-up your environment</span>
<span class="kw">rm</span>(cat)
<span class="kw">rm</span>(cat2)
<span class="co"># How can you get the original 'cat' object back?</span></code></pre></div>
<p><br></p>
<div class="tip">
<p><strong>HOORAY!</strong> Don’t worry about deleting data or making a mistake in R. When you load data files into R it only copies the contents. That means all your original data files will remain safe and won’t suffer from any accidental changes. If anything disappears or goes wrong in R, it’s okay! You can always re-load the data using your script. No more worries about whether you remembered to save the latest data file or not.</p>
</div>
<p><br></p>
<div id="give-it-a-name" class="section level2">
<h2><span class="header-section-number">2.1</span> Give it a name</h2>
<p>Everything has a name in R and you can name things almost anything you like. You can even name your data <code>TOP_SECRET_Shhhhhh...</code> or <code>unicorn_sightings</code> or <code>the_worst_data_ever</code>.</p>
<p>Sadly, there are a <em>few</em> minor restrictions. Object names can’t include spaces or special characters such as a <code>+</code>, <code>-</code>, <code>*</code>, <code>\</code>, <code>/</code>, <code>=</code>, <code>!</code>, or <code>)</code>. However, on the plus side object names can include a <code>_</code>.</p>
<p><br></p>
<p><strong>Your turn!</strong> <em>Try running some of these examples in your R console.</em></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">n cats <-<span class="st"> </span><span class="dv">5</span>
n<span class="op">*</span>cats <-<span class="st"> </span><span class="dv">5</span>
n_cats <-<span class="st"> </span><span class="dv">5</span>
n.cats <-<span class="st"> </span><span class="dv">5</span>
all_the_cats<span class="op">!</span><span class="st"> </span><span class="er"><</span><span class="op">-</span><span class="st"> "A very a big number"</span>
<span class="co"># You can add one cat</span>
n_cats <-<span class="st"> </span>n_cats <span class="op">+</span><span class="st"> </span><span class="dv">1</span>
<span class="co"># But what if you have 10,000 cats?</span>
n_cats <-<span class="st"> </span><span class="dv">10</span>,<span class="dv">000</span>
<span class="co"># They also cannot begin with a number.</span>
1st_cat <-<span class="st"> "Fluffer Puff"</span>
<span class="co"># But they can contain numbers.</span>
cat1 <-<span class="st"> "Fluffer Puff"</span></code></pre></div>
<p><br></p>
<blockquote>
<p><strong>NOTE:</strong> What happened when you created <code>n_cats</code> the second time?</p>
<p>When you create a new object that has the same name as something that already exists, the new object will replace the old one. Sometimes you’ll want to update an existing object and replace the old version. Other times you may want to copy an object to a new name to preserve the original. This is similar to choosing between <em>Save</em> and <em>Save As</em> when you save a file.</p>
</blockquote>
</div>
<div id="multiple-items" class="section level2">
<h2><span class="header-section-number">2.2</span> Multiple items</h2>
<hr>
<p>You can put multiple values inside <code>c()</code> to make a vector of items. Each additional item is separated by a comma. The <code>c</code> stands for to conCATenate or to combine values.</p>
<p>Let’s use <code>c()</code> to create a few vectors of cat names and their ages.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Create a character vector and name it cat_names</span>
cat_names <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"Fluffy"</span>, <span class="st">"Longmire"</span>, <span class="st">"Lucy"</span>)
<span class="co"># Print cat_names to the console</span>
cat_names</code></pre></div>
<pre><code>## [1] "Fluffy" "Longmire" "Lucy"</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Create a numeric vector and name it cat_ages</span>
cat_ages <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">3</span>,<span class="dv">7</span>,<span class="fl">14.5</span>)
<span class="co"># Print cat_ages to the console</span>
cat_ages</code></pre></div>
<pre><code>## [1] 3.0 7.0 14.5</code></pre>
</div>
<div id="make-a-table" class="section level2">
<h2><span class="header-section-number">2.3</span> Make a table</h2>
<p>A table in R is known as a <strong>data frame</strong>. Data frames have columns of data, each made from a named vector. Let’s make a data frame with two columns by using the cat names and ages from above.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Create table with columns "names" and "ages" with values from the cat_names and cat_ages vectors</span>
cat_df <-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">names =</span> cat_names,
<span class="dt">ages =</span> cat_ages)
<span class="co"># Print the cat_df data frame to the console</span>
cat_df</code></pre></div>
<pre><code>## names ages
## 1 Fluffy 3.0
## 2 Longmire 7.0
## 3 Lucy 14.5</code></pre>
<p><br></p>
<p>To see the values in one of your columns, use the <code>$</code> sign after the name of your table.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Print the "ages" column in cat_df</span>
cat_df<span class="op">$</span>ages</code></pre></div>
<pre><code>## [1] 3.0 7.0 14.5</code></pre>
<div class="quiz">
<h3 id="pop-quiz-hotshot" class="unnumbered">Pop Quiz, hotshot!</h3>
<p><strong>Which of these names are valid for a new object? (Hint: You are allowed to test them.)</strong></p>
<p><input type="radio"> <em>my cat fred</em> <br> <input type="radio"> <em>my_CAT55</em> <br> <input type="radio"> <em>5cats</em> <br> <input type="radio"> <em>my-cat</em> <br> <input type="radio"> <em>Whatever!</em> <br></p>
<br> <details> <summary class = "btn_code"><em>Show solution</em></summary>
<p>
<p><i class="fa fa-check" aria-hidden="true" style="color: green;"></i> <code>my_CAT55</code></p>
<p><em>Yes!! That was purrfect!</em></p>
</p>
</details>
</div>
</div>
<div id="leave-a-comment" class="section level2">
<h2><span class="header-section-number">2.4</span> Leave a #comment</h2>
<p>You may have noticed the text in the scripts with the <code>#</code> in front. These are called comments. Any line that starts with a <code>#</code> won’t be executed as R code. You can use the # to add notes in your script to make it easier for others and yourself to understand what is happening and why. You can also use comments to add warnings or instructions for others, add references to data you’re using, or point out things that need to be looked into further.</p>
</div>
<div id="functions" class="section level2">
<h2><span class="header-section-number">2.5</span> Functions</h2>
<div class="figure">
<img src="images/pizza_cat.png" align="right" style="margin-left:25px; margin-bottom: 60px;" width="280" />
</div>
<p>Now that you know what objects are and how to create them, let’s learn how to use them. Functions take one or more inputs called “arguments”. They perform steps based on the arguments and usually return an output object.</p>
<p><br></p>
<p>You can think of a function like ordering pizza.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">order_pizza</span>(<span class="dt">address =</span> <span class="st">"140 3rd place, Bingo MN"</span>,
<span class="dt">toppings =</span> <span class="kw">c</span>(<span class="st">"mouse whiskers"</span>, <span class="st">"catnip"</span>, <span class="st">"anchovies"</span>),
<span class="dt">time =</span> <span class="st">"ASAP"</span>)</code></pre></div>
<p><br></p>
<p>The function above calls a pizza place and provides several arguments (your address, pizza toppings, and delivery time). With some luck, the function will sucessfully return a new object (your cat’s favorite pizza). Note that when you have more than one argument, you will use a comma to separate them.</p>
<div id="mean-age" class="section level3 unnumbered">
<h3>Mean age</h3>
<p>We already covered two functions: <code>c()</code> and <code>data.frame()</code>. Now let’s use the <code>mean()</code> function to find the average age of our cats.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Call the mean function with cat_ages as input</span>
cat_ages_mean <-<span class="st"> </span><span class="kw">mean</span>(cat_ages) <span class="co"># Assigns the output to cat_ages_mean</span>
<span class="co"># Print the cat_ages_mean value to the console</span>
cat_ages_mean</code></pre></div>
<pre><code>## [1] 8.166667</code></pre>
<p><br></p>
<p>The <code>mean()</code> function takes the <em>cat_ages</em> vector as input, performs some calculations, and returns a single numeric object. Note that we assigned the output object to the name <code>cat_ages_mean</code>. If you don’t assign the output object it will be printed to the console and won’t be saved. Sometimes this is okay, especially when you’re still in the exploratory stages.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Alternative without assigning output</span>
<span class="kw">mean</span>(cat_ages) </code></pre></div>
<pre><code>## [1] 8.166667</code></pre>
<p><br></p>
<p>Note that our <code>cat_ages</code> vector has not changed at all. Each function has its own “environment”, and all of its calculations happen inside its own bubble. Usually anything that happens inside a function won’t change objects outside of the function’s environment.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">cat_ages</code></pre></div>
<pre><code>## [1] 3.0 7.0 14.5</code></pre>
<p><br></p>
</div>
<div id="function-summary" class="section level3 unnumbered">
<h3>Function summary</h3>
<p>There are functions in R that are more complex, but most boil down to the same general setup:</p>
<blockquote>
<p>new_output <- function(input1, input2)</p>
</blockquote>
<p>You call the function with input arguments inside parentheses and get an output object in return. You can make your own functions in R and call them almost anything you like, even <code>my_amazing_cat_function()</code>. Naming rules for functions and arguments are the same as those for objects (no spaces or special characters like <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>\</code>, <code>=</code>, or <code>!</code> and they can’t begin with a number, sorry).</p>
<div class="quiz">
<h3 id="pop-quiz" class="unnumbered">Pop Quiz!</h3>
<p><strong>Which of these is a valid function call?</strong></p>
<p><input type="radio"> <em>lick(“paws” “tail”)</em> <br> <input type="radio"> <em>scratch, “couch”, “door”</em> <br> <input type="radio"> <em>sleep(3, “hours”)</em> <br> <input type="radio"> <em>meow(until fed)</em> <br> <input type="radio"> <em>shed(1 million, “hairs”)</em> <br></p>
<br> <details> <summary class = "btn_code"><em>Show solution</em></summary>
<p>
<p><i class="fa fa-check" aria-hidden="true" style="color: green;"></i> <code>sleep(3, "hours")</code></p>
<p><em>Correct! You’re quite good at this.</em></p>
</p>
</details>
</div>
</div>
</div>
</div>
<div id="reading-and-loading-data" class="section level1">
<h1><span class="header-section-number">3</span> | Reading and loading data</h1>
<hr>
<p>The first step of a good mystery is finding some clues. Here’s an example data table showing my favorite cats. It is saved on the internet as an Excel file <a href="https://raw.githubusercontent.com/MPCA-air/RCamp/master/data/my_cats.xlsx">here</a>.</p>
<table>
<thead>
<tr class="header">
<th align="left">Cat Name</th>
<th align="right">Cat Age</th>
<th align="left">Cat Color</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Mr. Sauce</td>
<td align="right">3</td>
<td align="left">Salt-n-pepper</td>
</tr>
<tr class="even">
<td align="left">Sad Face</td>
<td align="right">2</td>
<td align="left">Tabby</td>
</tr>
<tr class="odd">
<td align="left">Noodles</td>
<td align="right">6</td>
<td align="left">Calico</td>
</tr>
</tbody>
</table>
<p><br></p>
<blockquote>
<p><strong>But how do we get this data into R?</strong></p>
</blockquote>
<div id="csv-to-the-rescue" class="section level2">
<h2><span class="header-section-number">3.1</span> CSV to the rescue</h2>
<p>The main data format in R is the <strong>CSV</strong> <em>(comma-separated values)</em>. A <strong>CSV</strong> is a simple text file that can be opened in R and most other stats software, including Excel.</p>
<p>Here’s how the example cat table looks when it is saved as a <strong>.CSV</strong> file.</p>
<p><strong>my_cats.csv</strong></p>
<pre><code>Cat Name,Cat Age,Cat Color
Mr. Sauce,3,Salt-n-pepper
Two Face,2,Tabby
Noodles,6,Calico </code></pre>
<p>It looks squished together right now, but that’s okay. When it’s opened in R the text will become a familiar looking table with columns and rows.</p>
<p><br></p>
</div>
<div id="save-excel-to-csv-file" class="section level2">
<h2><span class="header-section-number">3.2</span> Save Excel to CSV file</h2>
<p>First, open the Excel cat table by copying this path into a new window or to the Windows search bar - <code>X:\Agency_Files\Outcomes\Risk_Eval_Air_Mod\_Air_Risk_Evaluation\R\R_Camp\Student Folder\my_cats.xlsx</code>.</p>
<p>And then follow these instructions to save the Excel file as a <em>CSV</em> file.</p>
<ul>
<li>Go to <em>File</em><br />
</li>
<li><em>Save As</em><br />
</li>
<li><em>Browse</em> to your project folder<br />
</li>
<li>Create new “data” folder<br />
</li>
<li>Save as type: _CSV (Comma Delimited) (*.csv)_
<ul>
<li>Any of the <em>CSV</em> options will work<br />
</li>
</ul></li>
<li>Click <strong>Yes</strong><br />
</li>
<li>Close Excel (Click “Don’t Save”)</li>
</ul>
<p><br></p>
<p>Now let’s check that it worked. Return to RStudio and open your new data folder by finding it in your <em>Files</em> tab in the lower right window. Click on your <em>CSV</em> file and choose <strong>View File</strong>.</p>
</div>
<div id="read-csv-into-r" class="section level2">
<h2><span class="header-section-number">3.3</span> Read CSV into R</h2>
<p>Copy the code below to your R script and run the line with the <code>read.csv</code> function (Hit <strong>CTRL + ENTER</strong>). It will return a nice cat data frame called <code>my_cats</code>. The character string <code>"data/my_cats.csv"</code> inside the parentheses of the <code>read.csv()</code> function is the path of the data file.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">read.csv</span>(<span class="st">"data/my_cats.csv"</span>)</code></pre></div>
<pre><code>## Cat.Name Cat.Age Cat.Color
## 1 Mr. Sauce 3 Salt-n-pepper
## 2 Sad Face 2 Tabby
## 3 Noodles 6 Calico</code></pre>
<p><br></p>
<p><strong>Note:</strong> The location of the CSV file above will only work if you have your project open. When your project is open, R sets the working directory automatically to your project folder. We will demonstrate reading a file directly from the X-drive a bit later.</p>
<p><br></p>
</div>
<div id="name-your-table" class="section level2">
<h2><span class="header-section-number">3.4</span> Name your table</h2>
<p>If you want to work with the data in R, you will need to give it a name by using the assignment operator <code><-</code>.</p>
<p>Try the code below.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">my_cat_file <-<span class="st"> "data/my_cats.csv"</span>
my_cats <-<span class="st"> </span><span class="kw">read.csv</span>(my_cat_file)
<span class="co"># Type the name of the table to view it in the console</span>
my_cats</code></pre></div>
<pre><code>## Cat.Name Cat.Age Cat.Color
## 1 Mr. Sauce 3 Salt-n-pepper
## 2 Sad Face 2 Tabby
## 3 Noodles 6 Calico</code></pre>
</div>
<div id="bonus" class="section level2">
<h2><span class="header-section-number">3.5</span> Bonus</h2>
<p>You can save the file path as an object, such as <code>cat_file <- "data/my_cats.csv"</code>. Then you can use that object as a shortcut to the location of your data. Now when you want to load the cat table you can write <code>read.csv(cat_file)</code>. This handy trick will make it easier down the road when you want to update your code to use with new data.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Assign the file path character string as an object with the name cat_file.</span>
cat_file <-<span class="st"> "data/my_cats.csv"</span>
<span class="co"># Use read.csv with the object cat_file which refers to "data/my_cats.csv"</span>
<span class="kw">read.csv</span>(cat_file)</code></pre></div>
<pre><code>## Cat.Name Cat.Age Cat.Color
## 1 Mr. Sauce 3 Salt-n-pepper
## 2 Sad Face 2 Tabby
## 3 Noodles 6 Calico</code></pre>
</div>
</div>
<div id="add-a-new-package" class="section level1">
<h1><span class="header-section-number">4</span> | Add a new <code>package</code> 📦</h1>
<hr>
<blockquote>
<p><strong>What is a package?</strong></p>
</blockquote>
<p>A <em>package</em> is a small add-on for R, like a phone App for your phone. They add capabilities like statistical functions, mapping powers, and special charts. In order to use a new package we first need to install it.</p>
<div id="readr" class="section level2">
<h2><span class="header-section-number">4.1</span> <em>readr</em></h2>
<div class="figure">
<img src="https://d21ii91i3y6o6h.cloudfront.net/gallery_images/from_proof/9289/medium/1447092171/readr-hexbin-sticker-from-rstudio.png" align="left" style="margin-right:18px;" width="145" />
</div>
<p><br></p>
<p>The <em>readr</em> package helps import data into R in different formats. It does extra work for you like cleaning the data of extra white space and formatting tricky dates. Your packages are stored in your R <em>library</em>.</p>
<p><br> <br></p>
<p><strong>Add a package to your library</strong></p>
<ol style="list-style-type: decimal">
<li>Open <em>RStudio</em></li>
<li>Type <code>install.packages("readr")</code> in the lower left console</li>
<li>Press Enter</li>
<li><em>Wait two seconds</em></li>
<li>Open the <code>Packages</code> tab in the lower right window of RStudio to see the packages in your <em>library</em>
<ul>
<li>Use the search bar to find the <code>readr</code> package</li>
</ul></li>
</ol>
<p><br></p>
<p>The packages tab only shows the available packges that are installed. To use one of them, you will need to load it. Loading a package is like opening an App on your phone. To load a package we need to use the <code>library()</code> function. After loading the <em>readr</em> package you will able to read the cat data with the shiny new function <code>read_csv()</code>. This function is 300% better than <code>read</code>.<code>csv()</code>.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">"readr"</span>)
my_cat_file <-<span class="st"> "data/my_cats.csv"</span>
<span class="kw">read_csv</span>(my_cat_file)</code></pre></div>
<pre><code>## Parsed with column specification:
## cols(
## `Cat Name` = col_character(),
## `Cat Age` = col_integer(),
## `Cat Color` = col_character()
## )</code></pre>
<pre><code>## # A tibble: 3 x 3
## `Cat Name` `Cat Age` `Cat Color`
## <chr> <int> <chr>
## 1 Mr. Sauce 3 Salt-n-pepper
## 2 Sad Face 2 Tabby
## 3 Noodles 6 Calico</code></pre>
<p><br></p>
<div id="pro-tip" class="section level3 unnumbered">
<h3><i class="fa fa-user-secret" aria-hidden="true" style="color:#040707;"></i> Pro-tip!</h3>
<p>You may have noticed the row of three letter abbreviations under the column names. These describe the data type of each column.</p>
<blockquote>
<p><code>chr</code> stands for <strong>character</strong> vector, or a string of characters. Examples: <em>“apple”</em>, <em>“apple5”</em>, <em>“5 red apples”</em><br />
<code>int</code> stands for <strong>integer</strong>. Examples: <em>5</em>, <em>34</em>, <em>1071</em></p>
</blockquote>
<p>We’ll see more data types, such as <code>dates</code> and <code>logical</code>, in later lessons.</p>
<div class="quiz">
<h3 id="pop-quiz-1" class="unnumbered">Pop Quiz!</h3>
<p><strong>What data type is the <code>Color</code> column?</strong></p>
<p><input type="radio"> <em>letters</em> <br> <input type="radio"> <em>character</em> <br> <input type="radio"> <em>words</em> <br> <input type="radio"> <em>numbers</em> <br> <input type="radio"> <em>integer</em> <br></p>
<p><br></p>
<details> <summary class = "btn_code"><em>Show solution</em></summary>
<p>
<p><i class="fa fa-check" aria-hidden="true" style="color: green;"></i> <code>character</code></p>
<pre><code>## Colors cannot be applied in this environment :( Try using a terminal or RStudio.</code></pre>
<pre><code>##
## --------------
## Meow! Not too shabby for a tabby.
## --------------
## \
## \
## \
## |\___/|
## ) (
## =\ /=
## )===(
## / \
## | |
## / \
## \ /
## jgs \__ _/
## ( (
## ) )
## (_(
## </code></pre>
</p>
</details>
</div>
</div>
<div id="get-help-on-functions" class="section level3">
<h3><span class="header-section-number">4.1.1</span> Get help on functions</h3>
<p>Let’s look a bit closer at the <code>read_csv()</code> function.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">read_csv</span>(my_cat_file)
<span class="co"># Get help </span>
?read_csv</code></pre></div>
<p><br></p>
<p><strong>Function arguments</strong></p>
<p>For <code>read_csv()</code>, the character object argument <em>my_cat_file</em> is what the function uses to know where to find the data file to read. Funcitons often have more than one argument. Type <code>?read_csv</code> into your console to see help in the lower-right pane that describes all of the function’s arguments and what they do. Many of the options have default arguments (such as <code>col_names = TRUE</code>), which the function will use if you don’t provide an alternative argument. A short scroll down in the help window will show you more details about the arguments and the values they take.</p>
<blockquote>
<p>`function(arg1 = input1, arg2 = input2, arg3…)</p>
</blockquote>
<p>The <em>file</em> argument tells us that the function expects a path to a file. It can be many types of files, even a <em>ZIP</em> file. Below that, you’ll see the <em>col_names</em> argument. This argument takes either <code>TRUE</code>, <code>FALSE</code>, or a character vector of column names. The default is <code>TRUE</code>, which means the first row in the CSV is used as the column names for your data.</p>
<p><strong>Don’t like the column names?</strong> We can give new column names to the <em>col_names</em> argument like this:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">my_cat_file <-<span class="st"> "data/my_cats.csv"</span>
<span class="co">#Assign desired column names as a character vector named column_names</span>
column_names <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"Name"</span>, <span class="st">"Age"</span>, <span class="st">"Color"</span>)
<span class="kw">read_csv</span>(my_cat_file, column_names)</code></pre></div>
<pre><code>## # A tibble: 4 x 3
## Name Age Color
## <chr> <chr> <chr>
## 1 Cat Name Cat Age Cat Color
## 2 Mr. Sauce 3 Salt-n-pepper
## 3 Sad Face 2 Tabby
## 4 Noodles 6 Calico</code></pre>
<p><br></p>
<p>We now have the column names we want, but now the original column names in our CSV file show up as a row in our data. We want <code>read_csv</code> to ignore the first row. Let’s look through the help window and try to find an argument that can help us. The <code>skip</code> argument looks like it could be helpful. Sure enough, the description is exactly what we’re looking for here. The default is <code>skip = 0</code> (read every line), but we can skip the first line by providing <code>skip = 1</code>.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">column_names <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"Name"</span>, <span class="st">"Age"</span>, <span class="st">"Color"</span>)
<span class="kw">read_csv</span>(my_cat_file, column_names, <span class="dt">skip =</span> <span class="dv">1</span>)</code></pre></div>
<pre><code>## Parsed with column specification:
## cols(
## Name = col_character(),
## Age = col_integer(),
## Color = col_character()
## )</code></pre>
<pre><code>## # A tibble: 3 x 3
## Name Age Color
## <chr> <int> <chr>
## 1 Mr. Sauce 3 Salt-n-pepper
## 2 Sad Face 2 Tabby
## 3 Noodles 6 Calico</code></pre>
<p><br></p>
<p><strong>Success!</strong></p>
<p>You may be wondering why we included <code>skip =</code> for the skip argument, but only provided the objects for the other two arguments. When you pass inputs to a function, R will assume you’ve entered them in the same order that is shown on the ?help page. Let’s say you had a function called <code>feed_pets()</code> with 3 arguments:</p>
<blockquote>
<p><code>feed_pets(dogs = "dogfood", cats = "catnip", fish = "pellets")</code>.</p>
</blockquote>
<p>A shorthand way to write this would be <code>feed_pets("dogfood", "catnip", "pellets")</code>. If we write <code>feed_pets("dogfood", "pellets", "catnip")</code>, the function will send <em>fish pellets</em> to your cat and <em>catnip</em> to your fish. No good. If you really wanted to write “pellets” second, you would need to tell R which food item belongs to each animal, such as <code>feed_pets("dogfood", fish = "pellets", cats = "catnip")</code>.</p>
<p>The same thing goes for <code>read_csv()</code>. In <code>read_csv(my_cat_file, column_names, skip = 1)</code>, R assumes the file is <code>my_cat_file</code> and that the col_names should be set to <code>column_names</code>. The <code>skip =</code> argument has to be included explicitly because <em>skip</em> is the <strong>10th</strong> argument in <code>read_csv()</code>. If we don’t include <code>skip =</code>, R will assume the value we entered is meant for the function’s <strong>3rd</strong> argument.</p>
</div>
<div id="pro-tip-1" class="section level3 unnumbered">
<h3><i class="fa fa-user-secret" aria-hidden="true" style="color:#040707;"></i> Pro-tip!</h3>
<p>A handy shortcut to see the arguments of a function is to enter the name of the function in the console and the first parenthesis, such as <code>read_csv(</code>, and then hit <code>TAB</code> on the keyboard. This will bring up a drop-down menu of all the available arguments for that function.</p>
<p><br></p>
<p><strong>Key terms</strong></p>
<p><code>package</code> An add-on for R that contains new functions someone created to help you. It’s like an App for R.<br />
<code>library</code> The name of the folder that stores all your packages, and the function used to load a package.</p>
<div class="quiz">
<h3 id="pop-quiz-2" class="unnumbered">Pop Quiz!</h3>
<p><br></p>
<p><strong>What package does <code>read_csv()</code> come from?</strong></p>
<p><input type="radio"> <em>dinosaur</em> <br> <input type="radio"> <em>get_data</em> <br> <input type="radio"> <em>readr</em> <br> <input type="radio"> <em>dplyr</em> <br> <input type="radio"> <em>tidyr</em> <br></p>
<br> <details> <summary class = "btn_code"><em>Show solution</em></summary>
<p>
<p><i class="fa fa-check" aria-hidden="true" style="color: green;"></i> <code>readr</code></p>
<p><em>Great job! You’re fur real.</em></p>
</p>
<p></details></p>
<p><br></p>
<p><strong>How would you load the package <code>catfinder</code>?</strong></p>
<p><input type="radio"> <em>catfinder()</em> <br> <input type="radio"> <em>library(“catfinder”)</em> <br> <input type="radio"> <em>load(“catfinder”)</em> <br> <input type="radio"> <em>package(“catfinder”)</em> <br></p>
<br> <details> <summary class = "btn_code"><em>Show solution</em></summary>
<p>
<p><i class="fa fa-check" aria-hidden="true" style="color: green;"></i> <code>library("catfinder")</code></p>
<p><em>Excellent! Keep the streak going.</em></p>
</p>
</details>
</div>
</div>
</div>
</div>
<div id="data-exploration" class="section level1 unnumbered">
<h1>Data exploration</h1>
</div>
<div id="pet-detective" class="section level1">
<h1><span class="header-section-number">5</span> | Pet detective</h1>
<hr>
<div id="the-case-of-the-mopey-cat" class="section level2">
<h2><span class="header-section-number">5.1</span> <em>The case of the mopey cat</em></h2>
<p><img src="http://pre15.deviantart.net/653f/th/pre/i/2016/006/5/9/cat_in_the_rain_by_northumbrianartist-d9mzdda.jpg", align="right" width="250" style="margin-left:15px;"></p>
<p>On your way home from work you find a <em>wet</em> and <em>mopey</em> cat sitting on your front stoop. Oh my! It looks so sad, maybe you can bring it inside and give it a treat.</p>
<p>When you pick up the cat you notice a collar with a <strong>number</strong> on it.</p>
<blockquote>
<p><em>HINT: Your cat’s tag number is the same as the last digit of your birthday. Weird coincidence right?</em></p>
</blockquote>
<p>This gives you an idea! Your friend recently mentioned a list that has all the missing cats people have reported in the city. Maybe you can use the tag number to help find the lost kitty’s home.</p>
<p><br></p>
</div>
<div id="its-inspector-time" class="section level2">
<h2><span class="header-section-number">5.2</span> It’s inspector time!</h2>
<div class="figure">
<img src="http://orig00.deviantart.net/cf6c/f/2017/054/b/6/inspector_gadget_by_domejohnny-db04gni.png" align="right" style="-webkit-transform: scaleX(-1); transform: scaleX(-1); margin-right: 80px; margin-top: -10px" width="250" />
</div>
<p>Let’s get our script ready for some detective work. Since we bill by the hour we’re going to be very thorough with our documentation.</p>
<p><strong>Script comments</strong></p>
<p>Add a brief description of your new case to the top of the script using the comment symbol <code>#</code>.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># This file documents my search for the home of a lost cat I found this evening.</span>
<span class="co"># What I know so far</span>
animal <-<span class="st"> "cat"</span>
tag_num <-<span class="st"> </span><span class="dv">444</span>
pet_detective <-<span class="st"> "Agent Cooper"</span>
<span class="co"># Next steps</span>
<span class="co"># 1. Find the missing cat database.</span>
<span class="co"># 2. Load the cat data.</span>
<span class="co">#</span></code></pre></div>
<p><br></p>
<p>Now let’s find ourselves some clues.</p>
</div>
</div>
<div id="all-the-cats" class="section level1">
<h1><span class="header-section-number">6</span> | All the cats!</h1>
<hr>
<p>Thanks to your friend, you can download a list of all the missing cats in your town. Follow the steps below to read the cat data into R.</p>
<div id="find-the-files-location" class="section level2 unnumbered">
<h2>Find the file’s location</h2>
<p>Open this URL in your browser <a href="https://github.com/MPCA-air/RCamp/tree/master/data" class="uri">https://github.com/MPCA-air/RCamp/tree/master/data</a>, and find the file <code>missing_cat_list.csv</code>. To download, <strong>Right click</strong> on your cat’s file and select <strong>Save Link As…</strong>. Navigate to your project folder and save the file into a folder named <code>data\</code>. Don’t have a <code>data</code> folder? Go ahead and create a new one.</p>
</div>
<div id="add-the-file-location-to-your-script" class="section level2 unnumbered">
<h2>Add the file location to your script</h2>
<p>Now you can paste the file’s location into the <code>read_csv()</code> function. Here’s a code snippet to get you started.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">"readr"</span>)
<span class="co"># Replace the `...` with the name of the file</span>
all_cats_file <-<span class="st"> "data/..."</span>
<span class="co"># Replace the `...` with 'all_cats_file'</span>
all_cats <-<span class="st"> </span><span class="kw">read_csv</span>(...)</code></pre></div>
<br> <details> <summary class = "btn_code"><em>Show solution</em></summary>
<p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">"readr"</span>)
all_cats_file <-<span class="st"> "X:/Agency_Files/Outcomes/Risk_Eval_Air_Mod/_Air_Risk_Evaluation/R/R_Camp/Student Folder/missing_cat_list.csv"</span>
all_cats <-<span class="st"> </span><span class="kw">read_csv</span>(all_cats_file)</code></pre></div>
<pre><code>## Parsed with column specification:
## cols(
## name = col_character(),
## color = col_character(),
## age = col_integer(),
## gender = col_character(),
## country = col_character(),
## grumpy = col_integer(),
## fearful_of_people = col_integer(),
## playful = col_integer(),
## friendly_to_people = col_integer(),
## clumsy = col_integer(),
## greedy = col_integer(),
## owner_phone = col_character()
## )</code></pre>
</p>
<p></details></p>
<br>
<div class="tip">
<h3 id="copy-a-files-location" class="unnumbered">Copy a file’s location</h3>
<p>In <em>Windows</em> there’s a handy trick to copy the path to a file on your computer:</p>
<ul>
<li>Hold Shift + Right click on the file name. Select “Copy as path” from the menu.</li>
</ul>
</div>
<div id="pro-tip-2" class="section level3 unnumbered">
<h3><i class="fa fa-user-secret" aria-hidden="true" style="color:#040707;"></i> Pro-tip!</h3>
<blockquote>
<p>File paths in R use forward slashes (<code>/</code>). In Windows you’ll need to switch backslashes (<code>\</code>) to forward slashes (<code>/</code>). A file on your desktop located at <code>C:\Desktop\file.csv</code> would be read into R as <code>read_csv("C:/Desktop/file.csv")</code></p>
<p>One trick to quickly fix a long path name is to press <strong>CTRL + F</strong>. Then you can use search tool to find any (<code>\</code>) and replace it with (<code>/</code>).</p>
</blockquote>
<p><br></p>
<p><strong>NOTE:</strong> It’s good practice to load the packages you will need at the top of your script. You will need to run these lines <strong>every</strong> time you open R or switch projects. If you forget, you’re likely to see this error message.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">read_csv</span>(my_cat_file)</code></pre></div>
<pre><code>## Error in read_csv(my_cat_file): could not find function "read_csv"</code></pre>
<p><br></p>
<div class="figure">
<img src="images/errors.png" width="380" />
</div>
</div>
</div>
<div id="view-the-cats" class="section level2 unnumbered">
<h2>View the cats</h2>
<p>Look in the upper right hand window of RStudio. This is the <em>Environment</em> window that shows all of the data frames you have created this session. You can see the names of the data frames, the number of observations (rows), and the number of variables (columns). This is helpful if you ever need to count your data (<em>Hint</em> <em>Hint</em>).</p>
<p>To see <strong>all</strong> the cat data click on the table called <code>all_cats</code> in the <em>Environment</em> window.</p>
<blockquote>
<p>“There’s over 2,000 missing cats!”</p>
</blockquote>
<p>That’s a lot of cats. And there’s more bad news. There’s no column for tag numbers. So which cat is yours? Let’s see… Does your cat look more like a <em>Mr. Buttons</em> or a <em>Furry Potter</em>? That’s a tough call. We need more information!</p>
<p>Let’s explore the missing cat data a bit more. Hopefully we can find some way to pick out your cat.</p>
<div class="quiz">
<h3 id="pop-quiz-hotshot-1" class="unnumbered">Pop Quiz, hotshot!</h3>
<p><strong>What is the second column in the <code>all_cats</code> data frame?</strong></p>
<p><input type="radio"> <em>age</em> <br> <input type="radio"> <em>gender</em> <br> <input type="radio"> <em>nametag</em> <br> <input type="radio"> <em>color</em> <br> <input type="radio"> <em>fur</em> <br></p>
<br> <details> <summary class = "btn_code"><em>Show solution</em></summary>
<p>
<p><i class="fa fa-check" aria-hidden="true" style="color: green;"></i> <code>color</code></p>
<p><em>You sure aren’t kitten around! Great work!</em></p>
</p>
</details>
</div>
</div>
</div>
<div id="dplyr" class="section level1">
<h1><span class="header-section-number">7</span> | <strong>dplyr</strong></h1>
<hr>
<div class="figure">
<img src="https://d33wubrfki0l68.cloudfront.net/071952491ec4a6a532a3f70ecfa2507af4d341f9/c167c/images/hex-dplyr.png" align="left" style="margin-right: 20px; margin-top: 8px" width="120" />
</div>
<p><br></p>
<p><em>You’ve unlocked a new package!</em></p>
<p>The <em>dplyr</em> package is the go-to tool for exploring, re-arranging, and summarizing data.</p>
<p><br><br></p>
<p>Use <code>install.packages("dplyr")</code> to add <em>dplyr</em> to your detective library.</p>
<p><br></p>
<p><strong>Quick stretch break <i class="fa fa-hourglass-half" aria-hidden="true" style="color:#040707;"></i></strong></p>
<p>Stand up. Move around.</p>
<p><br></p>
<p><strong>Your analysis toolbox:</strong> The key <em>dplyr</em> functions</p>
<table>
<thead>
<tr class="header">
<th align="left">Function</th>
<th align="left">Returns</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left"><code>select()</code></td>
<td align="left">select individual columns to drop, keep, or reorder</td>
</tr>
<tr class="even">
<td align="left"><code>arrange()</code></td>
<td align="left">reorder or sort rows by value of a column</td>
</tr>
<tr class="odd">
<td align="left"><code>mutate()</code></td>
<td align="left">Add new columns or update existing columns</td>
</tr>
<tr class="even">
<td align="left"><code>filter()</code></td>
<td align="left">pick a subset of rows by the value of a column</td>
</tr>
<tr class="odd">
<td align="left"><code>group_by()</code></td>
<td align="left">split data into groups by values in a column</td>
</tr>