-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
2209 lines (2181 loc) · 304 KB
/
index.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.3.313">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Sam Csik">
<meta name="dcterms.date" content="2023-04-14">
<title>An iterative approach to streamlining your analytical workflows using functions and for loops</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 -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
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;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="index_files/libs/clipboard/clipboard.min.js"></script>
<script src="index_files/libs/quarto-html/quarto.js"></script>
<script src="index_files/libs/quarto-html/popper.min.js"></script>
<script src="index_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="index_files/libs/quarto-html/anchor.min.js"></script>
<link href="index_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="index_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="index_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="index_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="index_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
</head>
<body>
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article toc-left">
<div id="quarto-sidebar-toc-left" class="sidebar toc-left">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#how-is-this-document-organized" id="toc-how-is-this-document-organized" class="nav-link active" data-scroll-target="#how-is-this-document-organized"><svg aria-label="File lines" role="img" viewbox="0 0 384 512" style="height:1em;width:0.75em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>File lines</title><path d="M365.3 93.38l-74.63-74.64C278.6 6.742 262.3 0 245.4 0L64-.0001c-35.35 0-64 28.65-64 64l.0065 384c0 35.34 28.65 64 64 64H320c35.2 0 64-28.8 64-64V138.6C384 121.7 377.3 105.4 365.3 93.38zM336 448c0 8.836-7.164 16-16 16H64.02c-8.838 0-16-7.164-16-16L48 64.13c0-8.836 7.164-16 16-16h160L224 128c0 17.67 14.33 32 32 32h79.1V448zM96 280C96 293.3 106.8 304 120 304h144C277.3 304 288 293.3 288 280S277.3 256 264 256h-144C106.8 256 96 266.8 96 280zM264 352h-144C106.8 352 96 362.8 96 376s10.75 24 24 24h144c13.25 0 24-10.75 24-24S277.3 352 264 352z"></path></svg> How is this document organized?</a>
<ul class="collapse">
<li><a href="#sections" id="toc-sections" class="nav-link" data-scroll-target="#sections">Sections</a></li>
<li><a href="#see-what-im-googling" id="toc-see-what-im-googling" class="nav-link" data-scroll-target="#see-what-im-googling">See what I’m Googling!</a></li>
<li><a href="#check-out-the-new-quarto-code-annotations-feature" id="toc-check-out-the-new-quarto-code-annotations-feature" class="nav-link" data-scroll-target="#check-out-the-new-quarto-code-annotations-feature">Check out the new Quarto code annotations feature!</a></li>
</ul></li>
<li><a href="#about-the-data" id="toc-about-the-data" class="nav-link" data-scroll-target="#about-the-data"><svg aria-label="table" role="img" viewbox="0 0 512 512" style="height:1em;width:1em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>table</title><path d="M64 256V160H224v96H64zm0 64H224v96H64V320zm224 96V320H448v96H288zM448 256H288V160H448v96zM64 32C28.7 32 0 60.7 0 96V416c0 35.3 28.7 64 64 64H448c35.3 0 64-28.7 64-64V96c0-35.3-28.7-64-64-64H64z"></path></svg> About the Data</a></li>
<li><a href="#setup" id="toc-setup" class="nav-link" data-scroll-target="#setup"><svg aria-label="Folder Tree" role="img" viewbox="0 0 576 512" style="height:1em;width:1.12em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>Folder Tree</title><path d="M64 32C64 14.3 49.7 0 32 0S0 14.3 0 32v96V384c0 35.3 28.7 64 64 64H256V384H64V160H256V96H64V32zM288 192c0 17.7 14.3 32 32 32H544c17.7 0 32-14.3 32-32V64c0-17.7-14.3-32-32-32H445.3c-8.5 0-16.6-3.4-22.6-9.4L409.4 9.4c-6-6-14.1-9.4-22.6-9.4H320c-17.7 0-32 14.3-32 32V192zm0 288c0 17.7 14.3 32 32 32H544c17.7 0 32-14.3 32-32V352c0-17.7-14.3-32-32-32H445.3c-8.5 0-16.6-3.4-22.6-9.4l-13.3-13.3c-6-6-14.1-9.4-22.6-9.4H320c-17.7 0-32 14.3-32 32V480z"></path></svg> Setup</a></li>
<li><a href="#stage-1-clean-and-plot-one-data-set" id="toc-stage-1-clean-and-plot-one-data-set" class="nav-link" data-scroll-target="#stage-1-clean-and-plot-one-data-set"><strong><svg aria-label="Person walking" role="img" viewbox="0 0 320 512" style="height:1em;width:0.62em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>Person walking</title><path d="M256 48c0 26.5-21.5 48-48 48s-48-21.5-48-48s21.5-48 48-48s48 21.5 48 48zM126.5 199.3c-1 .4-1.9 .8-2.9 1.2l-8 3.5c-16.4 7.3-29 21.2-34.7 38.2l-2.6 7.8c-5.6 16.8-23.7 25.8-40.5 20.2s-25.8-23.7-20.2-40.5l2.6-7.8c11.4-34.1 36.6-61.9 69.4-76.5l8-3.5c20.8-9.2 43.3-14 66.1-14c44.6 0 84.8 26.8 101.9 67.9L281 232.7l21.4 10.7c15.8 7.9 22.2 27.1 14.3 42.9s-27.1 22.2-42.9 14.3L247 287.3c-10.3-5.2-18.4-13.8-22.8-24.5l-9.6-23-19.3 65.5 49.5 54c5.4 5.9 9.2 13 11.2 20.8l23 92.1c4.3 17.1-6.1 34.5-23.3 38.8s-34.5-6.1-38.8-23.3l-22-88.1-70.7-77.1c-14.8-16.1-20.3-38.6-14.7-59.7l16.9-63.5zM68.7 398l25-62.4c2.1 3 4.5 5.8 7 8.6l40.7 44.4-14.5 36.2c-2.4 6-6 11.5-10.6 16.1L54.6 502.6c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L68.7 398z"></path></svg> Stage 1:</strong> Clean and plot <em>one</em> data set</a>
<ul class="collapse">
<li><a href="#i.-load-libraries" id="toc-i.-load-libraries" class="nav-link" data-scroll-target="#i.-load-libraries"><strong>i.</strong> Load libraries</a></li>
<li><a href="#ii.-import-raw-data" id="toc-ii.-import-raw-data" class="nav-link" data-scroll-target="#ii.-import-raw-data"><strong>ii.</strong> Import raw data</a></li>
<li><a href="#iii.-clean-data" id="toc-iii.-clean-data" class="nav-link" data-scroll-target="#iii.-clean-data"><strong>iii.</strong> Clean data</a></li>
<li><a href="#iv.-plot-the-data" id="toc-iv.-plot-the-data" class="nav-link" data-scroll-target="#iv.-plot-the-data"><strong>iv.</strong> Plot the data</a></li>
</ul></li>
<li><a href="#stage-2-write-functions-to-clean-plot-your-data" id="toc-stage-2-write-functions-to-clean-plot-your-data" class="nav-link" data-scroll-target="#stage-2-write-functions-to-clean-plot-your-data"><strong><svg aria-label="Person running" role="img" viewbox="0 0 448 512" style="height:1em;width:0.88em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>Person running</title><path d="M336 48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM141.7 175.5c9.9-9.9 23.4-15.5 37.5-15.5c1.9 0 3.8 .1 5.6 .3L153.6 254c-9.3 28 1.7 58.8 26.8 74.5l86.2 53.9-25.4 88.8c-4.9 17 5 34.7 22 39.6s34.7-5 39.6-22l28.7-100.4c5.9-20.6-2.6-42.6-20.7-53.9L254 299l30.9-82.4 5.1 12.3C305 264.7 339.9 288 378.7 288H400c17.7 0 32-14.3 32-32s-14.3-32-32-32H378.7c-12.9 0-24.6-7.8-29.5-19.7l-6.3-15c-14.6-35.1-44.1-61.9-80.5-73.1l-48.7-15c-11.1-3.4-22.7-5.2-34.4-5.2c-31 0-60.8 12.3-82.7 34.3L73.4 153.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0l23.1-23.1zM107.2 352H48c-17.7 0-32 14.3-32 32s14.3 32 32 32h69.6c19 0 36.2-11.2 43.9-28.5L173 361.6l-9.5-6c-17.5-10.9-30.5-26.8-37.9-44.9L107.2 352z"></path></svg> Stage 2:</strong> Write functions to clean & plot your data</a>
<ul class="collapse">
<li><a href="#i.-write-a-function-to-clean-data-sets" id="toc-i.-write-a-function-to-clean-data-sets" class="nav-link" data-scroll-target="#i.-write-a-function-to-clean-data-sets"><strong>i.</strong> Write a function to <strong>clean</strong> data sets</a>
<ul class="collapse">
<li><a href="#version-1" id="toc-version-1" class="nav-link" data-scroll-target="#version-1"><strong>Version 1:</strong></a></li>
<li><a href="#version-2" id="toc-version-2" class="nav-link" data-scroll-target="#version-2"><strong>Version 2:</strong></a></li>
<li><a href="#version-3" id="toc-version-3" class="nav-link" data-scroll-target="#version-3"><strong>Version 3:</strong></a></li>
<li><a href="#version-4" id="toc-version-4" class="nav-link" data-scroll-target="#version-4"><strong>Version 4:</strong></a></li>
</ul></li>
<li><a href="#ii-write-a-function-to-plot-data-sets" id="toc-ii-write-a-function-to-plot-data-sets" class="nav-link" data-scroll-target="#ii-write-a-function-to-plot-data-sets"><strong>ii:</strong> Write a function to <strong>plot</strong> data sets</a>
<ul class="collapse">
<li><a href="#version-1-1" id="toc-version-1-1" class="nav-link" data-scroll-target="#version-1-1"><strong>Version 1:</strong></a></li>
<li><a href="#version-2-1" id="toc-version-2-1" class="nav-link" data-scroll-target="#version-2-1"><strong>Version 2:</strong></a></li>
</ul></li>
<li><a href="#iii-putting-it-all-together" id="toc-iii-putting-it-all-together" class="nav-link" data-scroll-target="#iii-putting-it-all-together"><strong>iii:</strong> Putting it all together</a></li>
</ul></li>
<li><a href="#stage-3-write-for-loops-to-read-in-clean-and-plot-all-your-data" id="toc-stage-3-write-for-loops-to-read-in-clean-and-plot-all-your-data" class="nav-link" data-scroll-target="#stage-3-write-for-loops-to-read-in-clean-and-plot-all-your-data"><strong><svg aria-label="rocket" role="img" viewbox="0 0 512 512" style="height:1em;width:1em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>rocket</title><path d="M156.6 384.9L125.7 354c-8.5-8.5-11.5-20.8-7.7-32.2c3-8.9 7-20.5 11.8-33.8L24 288c-8.6 0-16.6-4.6-20.9-12.1s-4.2-16.7 .2-24.1l52.5-88.5c13-21.9 36.5-35.3 61.9-35.3l82.3 0c2.4-4 4.8-7.7 7.2-11.3C289.1-4.1 411.1-8.1 483.9 5.3c11.6 2.1 20.6 11.2 22.8 22.8c13.4 72.9 9.3 194.8-111.4 276.7c-3.5 2.4-7.3 4.8-11.3 7.2v82.3c0 25.4-13.4 49-35.3 61.9l-88.5 52.5c-7.4 4.4-16.6 4.5-24.1 .2s-12.1-12.2-12.1-20.9V380.8c-14.1 4.9-26.4 8.9-35.7 11.9c-11.2 3.6-23.4 .5-31.8-7.8zM384 168c22.1 0 40-17.9 40-40s-17.9-40-40-40s-40 17.9-40 40s17.9 40 40 40z"></path></svg> Stage 3:</strong> Write for loops to read in, clean, and plot all your data</a>
<ul class="collapse">
<li><a href="#i.-write-a-for-loop-to-read-in-all-raw-data-files" id="toc-i.-write-a-for-loop-to-read-in-all-raw-data-files" class="nav-link" data-scroll-target="#i.-write-a-for-loop-to-read-in-all-raw-data-files"><strong>i.</strong> Write a for loop to <strong>read in</strong> all raw data files</a>
<ul class="collapse">
<li><a href="#step-1" id="toc-step-1" class="nav-link" data-scroll-target="#step-1"><strong>Step 1:</strong></a></li>
<li><a href="#step-2" id="toc-step-2" class="nav-link" data-scroll-target="#step-2"><strong>Step 2:</strong></a></li>
<li><a href="#step-3" id="toc-step-3" class="nav-link" data-scroll-target="#step-3"><strong>Step 3:</strong></a></li>
</ul></li>
<li><a href="#ii.-write-a-for-loop-to-clean-all-data" id="toc-ii.-write-a-for-loop-to-clean-all-data" class="nav-link" data-scroll-target="#ii.-write-a-for-loop-to-clean-all-data"><strong>ii.</strong> Write a for loop to <strong>clean</strong> all data</a>
<ul class="collapse">
<li><a href="#step-1-1" id="toc-step-1-1" class="nav-link" data-scroll-target="#step-1-1"><strong>Step 1:</strong></a></li>
<li><a href="#step-2-1" id="toc-step-2-1" class="nav-link" data-scroll-target="#step-2-1"><strong>Step 2:</strong></a></li>
<li><a href="#intermediate-step" id="toc-intermediate-step" class="nav-link" data-scroll-target="#intermediate-step"><strong>Intermediate Step:</strong></a></li>
<li><a href="#step-3-1" id="toc-step-3-1" class="nav-link" data-scroll-target="#step-3-1"><strong>Step 3:</strong></a></li>
</ul></li>
<li><a href="#iii.-write-a-for-loop-to-plot-all-data" id="toc-iii.-write-a-for-loop-to-plot-all-data" class="nav-link" data-scroll-target="#iii.-write-a-for-loop-to-plot-all-data"><strong>iii.</strong> Write a for loop to <strong>plot</strong> all data</a>
<ul class="collapse">
<li><a href="#step-1-2" id="toc-step-1-2" class="nav-link" data-scroll-target="#step-1-2"><strong>Step 1:</strong></a></li>
<li><a href="#step-2-2" id="toc-step-2-2" class="nav-link" data-scroll-target="#step-2-2"><strong>Step 2:</strong></a></li>
<li><a href="#step-3-2" id="toc-step-3-2" class="nav-link" data-scroll-target="#step-3-2"><strong>Step 3:</strong></a></li>
</ul></li>
<li><a href="#iv.-putting-it-all-together" id="toc-iv.-putting-it-all-together" class="nav-link" data-scroll-target="#iv.-putting-it-all-together"><strong>iv.</strong> Putting it all together</a></li>
</ul></li>
<li><a href="#review-resources" id="toc-review-resources" class="nav-link" data-scroll-target="#review-resources"><svg aria-label="Book open reader" role="img" viewbox="0 0 512 512" style="height:1em;width:1em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>Book open reader</title><path d="M352 96c0 53-43 96-96 96s-96-43-96-96s43-96 96-96s96 43 96 96zM240 248V512l-48.4-24.2c-20.9-10.4-43.5-17-66.8-19.3l-96-9.6C12.5 457.2 0 443.5 0 427V224c0-17.7 14.3-32 32-32H62.3c63.6 0 125.6 19.6 177.7 56zm32 264V248c52.1-36.4 114.1-56 177.7-56H480c17.7 0 32 14.3 32 32V427c0 16.4-12.5 30.2-28.8 31.8l-96 9.6c-23.2 2.3-45.9 8.9-66.8 19.3L272 512z"></path></svg> Review & Resources</a></li>
</ul>
</nav>
</div>
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
</div>
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">An iterative approach to streamlining your analytical workflows using functions and for loops</h1>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Author</div>
<div class="quarto-title-meta-contents">
<p>Sam Csik </p>
</div>
</div>
<div>
<div class="quarto-title-meta-heading">Published</div>
<div class="quarto-title-meta-contents">
<p class="date">April 14, 2023</p>
</div>
</div>
</div>
</header>
<p>By now, you may have heard instructors or online resources quote the saying, “If you’ve copied a block of code more than twice, you should probably consider writing a function.”</p>
<p>As your analyses get longer and more complex, the chances that you’ll benefit from writing functions will also increase – many times, this is to minimize the amount of code you need to copy/paste, but writing functions can also help to make your code:</p>
<ul>
<li>more navigable (e.g. by partitioning your code and functions into separate files)</li>
<li>more readable (e.g. creating human-readable function names that tell a user exactly what to expect)</li>
<li>less prone to errors (e.g. less copying/pasting large code chunks means less opportunity to introduce bugs)</li>
</ul>
<p>Iteration (i.e. performing the same operation on multiple inputs, e.g. multiple data sets, columns, etc.) is another tool for reducing duplication – for loops are one approach for performing iterative tasks. Together, both functions and for loops provide a powerful means to streamlining your analytical pipelines.</p>
<p><em>However</em>, whether you’re a new learner or seasoned programmer, it can sometimes feel overwhelming to know exactly where to start – especially when you have an end goal that builds additional complexity/flexibility into your functions and for loops. Taking an iterative approach to building out your code can help make this process clearer, less daunting, and more fun.</p>
<p>This document walks through <em>my</em> own iterative process for writing a couple functions and for loops to clean and visualize multiple, similar data sets. The way I’ve written this code is certainly not the <em>only</em> way (and likely not even the most optimized way), but I hope that by stepping through how I slowly build out my functions to be more flexible and user-friendly, it might help demonstrate an iterative workflow that I find particularly effective.</p>
<section id="how-is-this-document-organized" class="level1">
<h1><svg aria-label="File lines" role="img" viewbox="0 0 384 512" style="height:1em;width:0.75em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>File lines</title><path d="M365.3 93.38l-74.63-74.64C278.6 6.742 262.3 0 245.4 0L64-.0001c-35.35 0-64 28.65-64 64l.0065 384c0 35.34 28.65 64 64 64H320c35.2 0 64-28.8 64-64V138.6C384 121.7 377.3 105.4 365.3 93.38zM336 448c0 8.836-7.164 16-16 16H64.02c-8.838 0-16-7.164-16-16L48 64.13c0-8.836 7.164-16 16-16h160L224 128c0 17.67 14.33 32 32 32h79.1V448zM96 280C96 293.3 106.8 304 120 304h144C277.3 304 288 293.3 288 280S277.3 256 264 256h-144C106.8 256 96 266.8 96 280zM264 352h-144C106.8 352 96 362.8 96 376s10.75 24 24 24h144c13.25 0 24-10.75 24-24S277.3 352 264 352z"></path></svg> How is this document organized?</h1>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
This workshop is indended to be taught in-person
</div>
</div>
<div class="callout-body-container callout-body">
<p>I’ll be doing a lot of writing code, then testing code, then adding a bit more code, rinsing, and repeating. That can be a bit hard to capture in an instructional document, but I’ve done my best to outline my iterative steps here for folks to refer back to, as-needed.</p>
</div>
</div>
<section id="sections" class="level2">
<h2 class="anchored" data-anchor-id="sections">Sections</h2>
<p>This document follows just one case study – creating functions and for loops for processing and visualizing ocean temperature data from a few Santa Barbara Coastal LTER rocky reef sites. I do this in three “Stages”, each of which includes multiple steps/code versions:</p>
<p><strong><svg aria-label="Person walking" role="img" viewbox="0 0 320 512" style="height:1em;width:0.62em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>Person walking</title><path d="M256 48c0 26.5-21.5 48-48 48s-48-21.5-48-48s21.5-48 48-48s48 21.5 48 48zM126.5 199.3c-1 .4-1.9 .8-2.9 1.2l-8 3.5c-16.4 7.3-29 21.2-34.7 38.2l-2.6 7.8c-5.6 16.8-23.7 25.8-40.5 20.2s-25.8-23.7-20.2-40.5l2.6-7.8c11.4-34.1 36.6-61.9 69.4-76.5l8-3.5c20.8-9.2 43.3-14 66.1-14c44.6 0 84.8 26.8 101.9 67.9L281 232.7l21.4 10.7c15.8 7.9 22.2 27.1 14.3 42.9s-27.1 22.2-42.9 14.3L247 287.3c-10.3-5.2-18.4-13.8-22.8-24.5l-9.6-23-19.3 65.5 49.5 54c5.4 5.9 9.2 13 11.2 20.8l23 92.1c4.3 17.1-6.1 34.5-23.3 38.8s-34.5-6.1-38.8-23.3l-22-88.1-70.7-77.1c-14.8-16.1-20.3-38.6-14.7-59.7l16.9-63.5zM68.7 398l25-62.4c2.1 3 4.5 5.8 7 8.6l40.7 44.4-14.5 36.2c-2.4 6-6 11.5-10.6 16.1L54.6 502.6c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L68.7 398z"></path></svg> Stage 1:</strong> Clean and plot <em>one</em> data set – <em>always</em> my first step when beginning any analysis</p>
<ul>
<li>this is a standard workflow that includes reading in the data, then cleaning and plotting at least one data set (I include these steps for all three data sets, for reference…this involves lots of copying and pasting large chunks of code!)</li>
</ul>
<p><strong><svg aria-label="Person running" role="img" viewbox="0 0 448 512" style="height:1em;width:0.88em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>Person running</title><path d="M336 48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM141.7 175.5c9.9-9.9 23.4-15.5 37.5-15.5c1.9 0 3.8 .1 5.6 .3L153.6 254c-9.3 28 1.7 58.8 26.8 74.5l86.2 53.9-25.4 88.8c-4.9 17 5 34.7 22 39.6s34.7-5 39.6-22l28.7-100.4c5.9-20.6-2.6-42.6-20.7-53.9L254 299l30.9-82.4 5.1 12.3C305 264.7 339.9 288 378.7 288H400c17.7 0 32-14.3 32-32s-14.3-32-32-32H378.7c-12.9 0-24.6-7.8-29.5-19.7l-6.3-15c-14.6-35.1-44.1-61.9-80.5-73.1l-48.7-15c-11.1-3.4-22.7-5.2-34.4-5.2c-31 0-60.8 12.3-82.7 34.3L73.4 153.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0l23.1-23.1zM107.2 352H48c-17.7 0-32 14.3-32 32s14.3 32 32 32h69.6c19 0 36.2-11.2 43.9-28.5L173 361.6l-9.5-6c-17.5-10.9-30.5-26.8-37.9-44.9L107.2 352z"></path></svg> Stage 2:</strong> Write functions to clean & plot your data – getting a little fancier by turning long, repeated code chunks into functions</p>
<ul>
<li>Here I create two separate functions, one to clean data and one to plot data – for each, I take a super iterative approach (i.e. I start by creating a relatively simple function (that works!), then build in more flexibility/complexity)</li>
</ul>
<p><strong><svg aria-label="rocket" role="img" viewbox="0 0 512 512" style="height:1em;width:1em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>rocket</title><path d="M156.6 384.9L125.7 354c-8.5-8.5-11.5-20.8-7.7-32.2c3-8.9 7-20.5 11.8-33.8L24 288c-8.6 0-16.6-4.6-20.9-12.1s-4.2-16.7 .2-24.1l52.5-88.5c13-21.9 36.5-35.3 61.9-35.3l82.3 0c2.4-4 4.8-7.7 7.2-11.3C289.1-4.1 411.1-8.1 483.9 5.3c11.6 2.1 20.6 11.2 22.8 22.8c13.4 72.9 9.3 194.8-111.4 276.7c-3.5 2.4-7.3 4.8-11.3 7.2v82.3c0 25.4-13.4 49-35.3 61.9l-88.5 52.5c-7.4 4.4-16.6 4.5-24.1 .2s-12.1-12.2-12.1-20.9V380.8c-14.1 4.9-26.4 8.9-35.7 11.9c-11.2 3.6-23.4 .5-31.8-7.8zM384 168c22.1 0 40-17.9 40-40s-17.9-40-40-40s-40 17.9-40 40s17.9 40 40 40z"></path></svg> Stage 3:</strong> Write for loops to read in, clean, and plot all your data – streamline processing all of our data sets using less code</p>
<ul>
<li>Once I’ve created functions that help to reduce the code needed for processing and plotting my data, I write a few for loops to read in, clean, and plot all files (while this may seem a bit silly for just three files, imagine having to process/plot dozens or more!). Again, I do this in a few separate steps so I can ensure each small piece works before adding in the next bit.</li>
</ul>
</section>
<section id="see-what-im-googling" class="level2">
<h2 class="anchored" data-anchor-id="see-what-im-googling">See what I’m Googling!</h2>
<p>I realize that oftentimes, these carefully curated workshop materials may mask the amount of Googling / trial and error / general frustrations that I myself experience when building instructional content – I do <em>a lot</em> of Googling, and don’t know most of what I end up teaching off the top of my head. In an attempt to better normalize Googling for code help, I began saving some of the resources I referenced – they’re linked and noted with the <svg aria-label="Book open reader" role="img" viewbox="0 0 512 512" style="height:1em;width:1em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>Book open reader</title><path d="M352 96c0 53-43 96-96 96s-96-43-96-96s43-96 96-96s96 43 96 96zM240 248V512l-48.4-24.2c-20.9-10.4-43.5-17-66.8-19.3l-96-9.6C12.5 457.2 0 443.5 0 427V224c0-17.7 14.3-32 32-32H62.3c63.6 0 125.6 19.6 177.7 56zm32 264V248c52.1-36.4 114.1-56 177.7-56H480c17.7 0 32 14.3 32 32V427c0 16.4-12.5 30.2-28.8 31.8l-96 9.6c-23.2 2.3-45.9 8.9-66.8 19.3L272 512z"></path></svg> symbol. <strong>Disclaimer:</strong> I only thought to do this about half-way through, so the amount of reference materials is severely underestimated (just know that I’ve either had to look up to remind myself how to do most things, or do some serious digging to learn how to do something for the first time). I’ll try to be more consistent about including these little side notes moving forward!</p>
</section>
<section id="check-out-the-new-quarto-code-annotations-feature" class="level2">
<h2 class="anchored" data-anchor-id="check-out-the-new-quarto-code-annotations-feature">Check out the new Quarto code annotations feature!</h2>
<p>Line-based code annotations are a new feature of Quarto version 1.3, and I think they’re pretty nifty. Find numbered explanations beneath particular code chunks – clicking on a number will highlight the corresponding line of code. You can learn more about using annotations <a href="https://quarto.org/docs/authoring/code-annotation">here</a>.</p>
</section>
</section>
<section id="about-the-data" class="level1">
<h1><svg aria-label="table" role="img" viewbox="0 0 512 512" style="height:1em;width:1em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>table</title><path d="M64 256V160H224v96H64zm0 64H224v96H64V320zm224 96V320H448v96H288zM448 256H288V160H448v96zM64 32C28.7 32 0 60.7 0 96V416c0 35.3 28.7 64 64 64H448c35.3 0 64-28.7 64-64V96c0-35.3-28.7-64-64-64H64z"></path></svg> About the Data</h1>
<p>The Santa Barbara Coastal Long Term Ecological Research (SBC LTER) site was established in 2000 and is located in Santa Barbara’s coastal zone, where ocean currents and climate are highly variable with season and longer-term cycles. A number of kelp forest/rocky reef sites exist, where long-term data collection occurs using a variety of methods, including both repeated surveys and moored instrumentation. Today, we’ll be exploring ocean temperature data, collected via moored instrumentation (<a href="https://en.wikipedia.org/wiki/CTD_(instrument)#:~:text=A%20CTD%20or%20sonde%20is,is%20used%20to%20determine%20salinity.">CTD</a>s), from just three of these sites: Alegria Reef, Mohwak Reef, and Carpinteria Reef.</p>
<p>Raw data are available for download on the <a href="https://portal.edirepository.org/nis/home.jsp">EDI Data Portal</a>:</p>
<ol type="1">
<li>Alegria Reef: <a href="https://portal.edirepository.org/nis/mapbrowse?packageid=knb-lter-sbc.2008.14" class="uri">https://portal.edirepository.org/nis/mapbrowse?packageid=knb-lter-sbc.2008.14</a></li>
<li>Mohawk Reef: <a href="https://doi.org/10.6073/pasta/cbe43646b801bf6ee5231c301ea23f51" class="uri">https://doi.org/10.6073/pasta/cbe43646b801bf6ee5231c301ea23f51</a></li>
<li>Carpinteria Reef: <a href="https://portal.edirepository.org/nis/mapbrowse?packageid=knb-lter-sbc.2004.26" class="uri">https://portal.edirepository.org/nis/mapbrowse?packageid=knb-lter-sbc.2004.26</a></li>
</ol>
</section>
<section id="setup" class="level1">
<h1><svg aria-label="Folder Tree" role="img" viewbox="0 0 576 512" style="height:1em;width:1.12em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>Folder Tree</title><path d="M64 32C64 14.3 49.7 0 32 0S0 14.3 0 32v96V384c0 35.3 28.7 64 64 64H256V384H64V160H256V96H64V32zM288 192c0 17.7 14.3 32 32 32H544c17.7 0 32-14.3 32-32V64c0-17.7-14.3-32-32-32H445.3c-8.5 0-16.6-3.4-22.6-9.4L409.4 9.4c-6-6-14.1-9.4-22.6-9.4H320c-17.7 0-32 14.3-32 32V192zm0 288c0 17.7 14.3 32 32 32H544c17.7 0 32-14.3 32-32V352c0-17.7-14.3-32-32-32H445.3c-8.5 0-16.6-3.4-22.6-9.4l-13.3-13.3c-6-6-14.1-9.4-22.6-9.4H320c-17.7 0-32 14.3-32 32V480z"></path></svg> Setup</h1>
<ol type="1">
<li>create and clone a GitHub repository (be sure to add a <code>.gitignore</code> file)</li>
<li>download the data – these data are publicly available via the <a href="https://portal.edirepository.org/nis/home.jsp">EDI Data Portal</a>, but I’ve also added the necessary files to Google Drive for download <a href="https://drive.google.com/drive/folders/1S2IY-qo29CU9ahRj_QNfFLfrh7DnT3CG?usp=share_link">here</a> – these are <em>large</em> data files and take a few minutes to download; be sure to unzip the files once downloaded</li>
<li>add a subdirectory called <code>/data</code>; move your unzipped <code>raw_data</code> folder into the <code>/data</code> subdirectory; your folder structure should now look like: <code>your-repo/data/raw_data</code></li>
<li>add your <code>/raw_data</code> folder to your <code>.gitignore</code> file so that you don’t accidentally try pushing it to GitHub (these data files are far too large for that!) – to do so, scroll to the bottom of your <code>.gitignore</code>, type the following, <code>data/raw_data/</code>, and save; you can push this modified <code>.gitignore</code> file to GitHub now, if you’d like</li>
</ol>
</section>
<section id="stage-1-clean-and-plot-one-data-set" class="level1">
<h1><strong><svg aria-label="Person walking" role="img" viewbox="0 0 320 512" style="height:1em;width:0.62em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>Person walking</title><path d="M256 48c0 26.5-21.5 48-48 48s-48-21.5-48-48s21.5-48 48-48s48 21.5 48 48zM126.5 199.3c-1 .4-1.9 .8-2.9 1.2l-8 3.5c-16.4 7.3-29 21.2-34.7 38.2l-2.6 7.8c-5.6 16.8-23.7 25.8-40.5 20.2s-25.8-23.7-20.2-40.5l2.6-7.8c11.4-34.1 36.6-61.9 69.4-76.5l8-3.5c20.8-9.2 43.3-14 66.1-14c44.6 0 84.8 26.8 101.9 67.9L281 232.7l21.4 10.7c15.8 7.9 22.2 27.1 14.3 42.9s-27.1 22.2-42.9 14.3L247 287.3c-10.3-5.2-18.4-13.8-22.8-24.5l-9.6-23-19.3 65.5 49.5 54c5.4 5.9 9.2 13 11.2 20.8l23 92.1c4.3 17.1-6.1 34.5-23.3 38.8s-34.5-6.1-38.8-23.3l-22-88.1-70.7-77.1c-14.8-16.1-20.3-38.6-14.7-59.7l16.9-63.5zM68.7 398l25-62.4c2.1 3 4.5 5.8 7 8.6l40.7 44.4-14.5 36.2c-2.4 6-6 11.5-10.6 16.1L54.6 502.6c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L68.7 398z"></path></svg> Stage 1:</strong> Clean and plot <em>one</em> data set</h1>
<p>Copying, pasting, and slightly updating code to clean and process multiple similarly-structured data sets is certainly the slowest and most prone-to-errors workflow. However, (in <em>my</em> honest opinion) it’s almost always critical to being by processing one data set on it’s own before attempting to write a function to do so.</p>
<p>Let’s start by doing that here.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>I’ll be writing the following code in a script called, <code>no_functions_pipeline.R</code>, which I’ll save to my repo’s root directory.</p>
</div>
</div>
<section id="i.-load-libraries" class="level2">
<h2 class="anchored" data-anchor-id="i.-load-libraries"><strong>i.</strong> Load libraries</h2>
<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><span class="fu">library</span>(tidyverse)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(chron)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(naniar)</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggridges)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="ii.-import-raw-data" class="level2">
<h2 class="anchored" data-anchor-id="ii.-import-raw-data"><strong>ii.</strong> Import raw data</h2>
<p>Either download from the <a href="https://portal.edirepository.org/nis/home.jsp">Environmental Data Portal</a> (EDI) directly:</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><span class="co"># ale <- read_csv("https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-sbc.2008.14&entityid=15c25abf9bb72e2017301fa4e5b2e0d4")</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="co"># mko <- read_csv("https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-sbc.2007.16&entityid=02629ecc08a536972dec021f662428aa")</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="co"># car <- read_csv("https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-sbc.2004.26&entityid=1d7769e33145ba4f04aa0b0a3f7d4a76")</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Or read in the downloaded files from <code>data/raw_data/</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>alegria <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"alegria_mooring_ale_20210617.csv"</span>))</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>mohawk <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"mohawk_mooring_mko_20220330.csv"</span>))</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>carpinteria <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"carpinteria_mooring_car_20220330.csv"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="iii.-clean-data" class="level2">
<h2 class="anchored" data-anchor-id="iii.-clean-data"><strong>iii.</strong> Clean data</h2>
<p>Below, we select only the necessary columns (there are <em>far</em> too many (87) in the raw data), add a column for site name (the only way to tell which site the data were collected from is by looking at the file name), formatting dates/times, and replacing missing value codes with <em><code>NA</code></em>.</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>alegria_clean <span class="ot"><-</span> alegria <span class="sc">|></span> </span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># keep only necessary columns & filter for years 2005-2020</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(year, month, day, decimal_time, Temp_top, Temp_mid, Temp_bot) <span class="sc">|></span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(year <span class="sc">%in%</span> <span class="fu">c</span>(<span class="dv">2005</span><span class="sc">:</span><span class="dv">2020</span>)) <span class="sc">|></span> </span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># add column with site name</span></span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">site =</span> <span class="fu">rep</span>(<span class="st">"Alegria Reef"</span>)) <span class="sc">|></span> </span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a> <span class="co"># create date time column</span></span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date, year, month, day, <span class="at">sep =</span> <span class="st">"-"</span>, <span class="at">remove =</span> <span class="cn">FALSE</span>) <span class="sc">|></span> </span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">time =</span> <span class="fu">times</span>(decimal_time)) <span class="sc">|></span> </span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date_time, date, time, <span class="at">sep =</span> <span class="st">" "</span>, <span class="at">remove =</span> <span class="cn">TRUE</span>) <span class="sc">|></span> </span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a> <span class="co"># coerce data types</span></span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">date_time =</span> <span class="fu">as.POSIXct</span>(date_time, <span class="st">"%Y-%m-%d %H:%M:%S"</span>, <span class="at">tz =</span> <span class="st">"GMT"</span>),</span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a> <span class="at">year =</span> <span class="fu">as.factor</span>(year),</span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a> <span class="at">month =</span> <span class="fu">as.factor</span>(month),</span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a> <span class="at">day =</span> <span class="fu">as.numeric</span>(day)) <span class="sc">|></span></span>
<span id="cb4-20"><a href="#cb4-20" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb4-21"><a href="#cb4-21" aria-hidden="true" tabindex="-1"></a> <span class="co"># add month name (see: https://stackoverflow.com/questions/22058393/convert-a-numeric-month-to-a-month-abbreviation)</span></span>
<span id="cb4-22"><a href="#cb4-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">month_name =</span> <span class="fu">as.factor</span>(month.name[month])) <span class="sc">|></span></span>
<span id="cb4-23"><a href="#cb4-23" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb4-24"><a href="#cb4-24" aria-hidden="true" tabindex="-1"></a> <span class="co"># replace 9999s with NAs (will throw warning if var isn't present, but still execute)</span></span>
<span id="cb4-25"><a href="#cb4-25" aria-hidden="true" tabindex="-1"></a> <span class="fu">replace_with_na</span>(<span class="at">replace =</span> <span class="fu">list</span>(<span class="at">Temp_bot =</span> <span class="dv">9999</span>, <span class="at">Temp_top =</span> <span class="dv">9999</span>, <span class="at">Temp_mid =</span> <span class="dv">9999</span>)) <span class="sc">|></span> </span>
<span id="cb4-26"><a href="#cb4-26" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb4-27"><a href="#cb4-27" aria-hidden="true" tabindex="-1"></a> <span class="co"># select/reorder desired columns</span></span>
<span id="cb4-28"><a href="#cb4-28" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(site, date_time, year, month, day, month_name, Temp_bot, Temp_mid, Temp_top)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="iv.-plot-the-data" class="level2">
<h2 class="anchored" data-anchor-id="iv.-plot-the-data"><strong>iv.</strong> Plot the data</h2>
<p>Here, we create a ridge line plot (using the <code>{ggridges}</code> package) showing aggregate bottom temperatures (2005-2020), by month, at Alegria reef.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>alegria_plot <span class="ot"><-</span> alegria_clean <span class="sc">|></span> </span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># group by month ----</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(month_name) <span class="sc">|></span> </span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># plot ----</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> Temp_bot, <span class="at">y =</span> month_name, <span class="at">fill =</span> <span class="fu">after_stat</span>(x))) <span class="sc">+</span></span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_density_ridges_gradient</span>(<span class="at">rel_min_height =</span> <span class="fl">0.01</span>, <span class="at">scale =</span> <span class="dv">3</span>) <span class="sc">+</span> </span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">9</span>, <span class="dv">12</span>, <span class="dv">15</span>, <span class="dv">18</span>, <span class="dv">21</span>)) <span class="sc">+</span></span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_discrete</span>(<span class="at">limits =</span> <span class="fu">rev</span>(month.name)) <span class="sc">+</span> </span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_gradientn</span>(<span class="at">colors =</span> <span class="fu">c</span>(<span class="st">"#2C5374"</span>,<span class="st">"#778798"</span>, <span class="st">"#ADD8E6"</span>, <span class="st">"#EF8080"</span>, <span class="st">"#8B3A3A"</span>), <span class="at">name =</span> <span class="st">"Temp. (°C)"</span>) <span class="sc">+</span></span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Bottom Temperature (°C)"</span>,</span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Bottom Temperatures at Alegria Reef, Santa Barbara, CA"</span>,</span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a> <span class="at">subtitle =</span> <span class="st">"Temperatures (°C) aggregated by month from 2005 - 2020"</span>) <span class="sc">+</span></span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a> ggridges<span class="sc">::</span><span class="fu">theme_ridges</span>(<span class="at">font_size =</span> <span class="dv">13</span>, <span class="at">grid =</span> <span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.title.y =</span> <span class="fu">element_blank</span>()</span>
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb5-19"><a href="#cb5-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-20"><a href="#cb5-20" aria-hidden="true" tabindex="-1"></a>alegria_plot</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>If we were to continue with this workflow (which is absolutely a valid way that gets the job done!), we would need to repeat the above code two more times (for both the <code>mohawk</code> and <code>carpinteria</code> data frames) – this gets lengthy rather quickly, requires lots of copying/pasting, and is prone to errors (e.g. forgetting to update a data frame name, typos, etc.). If you’d like to check out the code for the <code>mohawk</code> and <code>carpinteria</code> data sets, unfold the code chunk below:</p>
<div>
<details>
<summary>Code</summary>
<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><span class="co">#..........................Mohawk Reef...........................</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="co"># clean ----</span></span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a>mohawk_clean <span class="ot"><-</span> mohawk <span class="sc">|></span> </span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(year, month, day, decimal_time, Temp_top, Temp_mid, Temp_bot) <span class="sc">|></span></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(year <span class="sc">%in%</span> <span class="fu">c</span>(<span class="dv">2005</span><span class="sc">:</span><span class="dv">2020</span>)) <span class="sc">|></span> </span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">site =</span> <span class="fu">rep</span>(<span class="st">"Mohawk Reef"</span>)) <span class="sc">|></span> </span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date, year, month, day, <span class="at">sep =</span> <span class="st">"-"</span>, <span class="at">remove =</span> <span class="cn">FALSE</span>) <span class="sc">|></span> </span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">time =</span> <span class="fu">times</span>(decimal_time)) <span class="sc">|></span> </span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date_time, date, time, <span class="at">sep =</span> <span class="st">" "</span>, <span class="at">remove =</span> <span class="cn">TRUE</span>) <span class="sc">|></span> </span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">date_time =</span> <span class="fu">as.POSIXct</span>(date_time, <span class="st">"%Y-%m-%d %H:%M:%S"</span>, <span class="at">tz =</span> <span class="st">"GMT"</span>),</span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a> <span class="at">year =</span> <span class="fu">as.factor</span>(year),</span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a> <span class="at">month =</span> <span class="fu">as.factor</span>(month),</span>
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a> <span class="at">day =</span> <span class="fu">as.numeric</span>(day)) <span class="sc">|></span></span>
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">month_name =</span> <span class="fu">as.factor</span>(month.name[month])) <span class="sc">|></span></span>
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">replace_with_na</span>(<span class="at">replace =</span> <span class="fu">list</span>(<span class="at">Temp_bot =</span> <span class="dv">9999</span>, <span class="at">Temp_top =</span> <span class="dv">9999</span>, <span class="at">Temp_mid =</span> <span class="dv">9999</span>)) <span class="sc">|></span> </span>
<span id="cb6-17"><a href="#cb6-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(site, date_time, year, month, day, month_name, Temp_bot, Temp_mid, Temp_top)</span>
<span id="cb6-18"><a href="#cb6-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-19"><a href="#cb6-19" aria-hidden="true" tabindex="-1"></a><span class="co"># plot ----</span></span>
<span id="cb6-20"><a href="#cb6-20" aria-hidden="true" tabindex="-1"></a>mohawk_plot <span class="ot"><-</span> mohawk_clean <span class="sc">|></span> </span>
<span id="cb6-21"><a href="#cb6-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(month_name) <span class="sc">|></span> </span>
<span id="cb6-22"><a href="#cb6-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> Temp_bot, <span class="at">y =</span> month_name, <span class="at">fill =</span> <span class="fu">after_stat</span>(x))) <span class="sc">+</span></span>
<span id="cb6-23"><a href="#cb6-23" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_density_ridges_gradient</span>(<span class="at">rel_min_height =</span> <span class="fl">0.01</span>, <span class="at">scale =</span> <span class="dv">3</span>) <span class="sc">+</span> </span>
<span id="cb6-24"><a href="#cb6-24" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">9</span>, <span class="dv">12</span>, <span class="dv">15</span>, <span class="dv">18</span>, <span class="dv">21</span>)) <span class="sc">+</span></span>
<span id="cb6-25"><a href="#cb6-25" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_discrete</span>(<span class="at">limits =</span> <span class="fu">rev</span>(month.name)) <span class="sc">+</span> </span>
<span id="cb6-26"><a href="#cb6-26" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_gradientn</span>(<span class="at">colors =</span> <span class="fu">c</span>(<span class="st">"#2C5374"</span>,<span class="st">"#778798"</span>, <span class="st">"#ADD8E6"</span>, <span class="st">"#EF8080"</span>, <span class="st">"#8B3A3A"</span>), <span class="at">name =</span> <span class="st">"Temp. (°C)"</span>) <span class="sc">+</span></span>
<span id="cb6-27"><a href="#cb6-27" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Bottom Temperature (°C)"</span>,</span>
<span id="cb6-28"><a href="#cb6-28" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Bottom Temperatures at Mohawk Reef, Santa Barbara, CA"</span>,</span>
<span id="cb6-29"><a href="#cb6-29" aria-hidden="true" tabindex="-1"></a> <span class="at">subtitle =</span> <span class="st">"Temperatures (°C) aggregated by month from 2005 - 2020"</span>) <span class="sc">+</span></span>
<span id="cb6-30"><a href="#cb6-30" aria-hidden="true" tabindex="-1"></a> ggridges<span class="sc">::</span><span class="fu">theme_ridges</span>(<span class="at">font_size =</span> <span class="dv">13</span>, <span class="at">grid =</span> <span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb6-31"><a href="#cb6-31" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb6-32"><a href="#cb6-32" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.title.y =</span> <span class="fu">element_blank</span>()</span>
<span id="cb6-33"><a href="#cb6-33" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb6-34"><a href="#cb6-34" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-35"><a href="#cb6-35" aria-hidden="true" tabindex="-1"></a>mohawk_plot</span>
<span id="cb6-36"><a href="#cb6-36" aria-hidden="true" tabindex="-1"></a><span class="co">#........................Carpinteria Reef........................</span></span>
<span id="cb6-37"><a href="#cb6-37" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-38"><a href="#cb6-38" aria-hidden="true" tabindex="-1"></a><span class="co"># clean ----</span></span>
<span id="cb6-39"><a href="#cb6-39" aria-hidden="true" tabindex="-1"></a>carpinteria_clean <span class="ot"><-</span> carpinteria <span class="sc">|></span> </span>
<span id="cb6-40"><a href="#cb6-40" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(year, month, day, decimal_time, Temp_top, Temp_mid, Temp_bot) <span class="sc">|></span></span>
<span id="cb6-41"><a href="#cb6-41" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(year <span class="sc">%in%</span> <span class="fu">c</span>(<span class="dv">2005</span><span class="sc">:</span><span class="dv">2020</span>)) <span class="sc">|></span> </span>
<span id="cb6-42"><a href="#cb6-42" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">site =</span> <span class="fu">rep</span>(<span class="st">"Carpinteria Reef"</span>)) <span class="sc">|></span> </span>
<span id="cb6-43"><a href="#cb6-43" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date, year, month, day, <span class="at">sep =</span> <span class="st">"-"</span>, <span class="at">remove =</span> <span class="cn">FALSE</span>) <span class="sc">|></span> </span>
<span id="cb6-44"><a href="#cb6-44" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">time =</span> <span class="fu">times</span>(decimal_time)) <span class="sc">|></span> </span>
<span id="cb6-45"><a href="#cb6-45" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date_time, date, time, <span class="at">sep =</span> <span class="st">" "</span>, <span class="at">remove =</span> <span class="cn">TRUE</span>) <span class="sc">|></span> </span>
<span id="cb6-46"><a href="#cb6-46" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">date_time =</span> <span class="fu">as.POSIXct</span>(date_time, <span class="st">"%Y-%m-%d %H:%M:%S"</span>, <span class="at">tz =</span> <span class="st">"GMT"</span>),</span>
<span id="cb6-47"><a href="#cb6-47" aria-hidden="true" tabindex="-1"></a> <span class="at">year =</span> <span class="fu">as.factor</span>(year),</span>
<span id="cb6-48"><a href="#cb6-48" aria-hidden="true" tabindex="-1"></a> <span class="at">month =</span> <span class="fu">as.factor</span>(month),</span>
<span id="cb6-49"><a href="#cb6-49" aria-hidden="true" tabindex="-1"></a> <span class="at">day =</span> <span class="fu">as.numeric</span>(day)) <span class="sc">|></span></span>
<span id="cb6-50"><a href="#cb6-50" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">month_name =</span> <span class="fu">as.factor</span>(month.name[month])) <span class="sc">|></span></span>
<span id="cb6-51"><a href="#cb6-51" aria-hidden="true" tabindex="-1"></a> <span class="fu">replace_with_na</span>(<span class="at">replace =</span> <span class="fu">list</span>(<span class="at">Temp_bot =</span> <span class="dv">9999</span>, <span class="at">Temp_top =</span> <span class="dv">9999</span>, <span class="at">Temp_mid =</span> <span class="dv">9999</span>)) <span class="sc">|></span> </span>
<span id="cb6-52"><a href="#cb6-52" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(site, date_time, year, month, day, month_name, Temp_bot, Temp_mid, Temp_top)</span>
<span id="cb6-53"><a href="#cb6-53" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-54"><a href="#cb6-54" aria-hidden="true" tabindex="-1"></a><span class="co"># plot ----</span></span>
<span id="cb6-55"><a href="#cb6-55" aria-hidden="true" tabindex="-1"></a>carpinteria_plot <span class="ot"><-</span> carpinteria_clean <span class="sc">|></span> </span>
<span id="cb6-56"><a href="#cb6-56" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(month_name) <span class="sc">|></span> </span>
<span id="cb6-57"><a href="#cb6-57" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> Temp_bot, <span class="at">y =</span> month_name, <span class="at">fill =</span> <span class="fu">after_stat</span>(x))) <span class="sc">+</span></span>
<span id="cb6-58"><a href="#cb6-58" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_density_ridges_gradient</span>(<span class="at">rel_min_height =</span> <span class="fl">0.01</span>, <span class="at">scale =</span> <span class="dv">3</span>) <span class="sc">+</span> </span>
<span id="cb6-59"><a href="#cb6-59" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">9</span>, <span class="dv">12</span>, <span class="dv">15</span>, <span class="dv">18</span>, <span class="dv">21</span>)) <span class="sc">+</span></span>
<span id="cb6-60"><a href="#cb6-60" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_discrete</span>(<span class="at">limits =</span> <span class="fu">rev</span>(month.name)) <span class="sc">+</span> </span>
<span id="cb6-61"><a href="#cb6-61" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_gradientn</span>(<span class="at">colors =</span> <span class="fu">c</span>(<span class="st">"#2C5374"</span>,<span class="st">"#778798"</span>, <span class="st">"#ADD8E6"</span>, <span class="st">"#EF8080"</span>, <span class="st">"#8B3A3A"</span>), <span class="at">name =</span> <span class="st">"Temp. (°C)"</span>) <span class="sc">+</span></span>
<span id="cb6-62"><a href="#cb6-62" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Bottom Temperature (°C)"</span>,</span>
<span id="cb6-63"><a href="#cb6-63" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Bottom Temperatures at Carpinteria Reef, Santa Barbara, CA"</span>,</span>
<span id="cb6-64"><a href="#cb6-64" aria-hidden="true" tabindex="-1"></a> <span class="at">subtitle =</span> <span class="st">"Temperatures (°C) aggregated by month from 2005 - 2020"</span>) <span class="sc">+</span></span>
<span id="cb6-65"><a href="#cb6-65" aria-hidden="true" tabindex="-1"></a> ggridges<span class="sc">::</span><span class="fu">theme_ridges</span>(<span class="at">font_size =</span> <span class="dv">13</span>, <span class="at">grid =</span> <span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb6-66"><a href="#cb6-66" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb6-67"><a href="#cb6-67" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.title.y =</span> <span class="fu">element_blank</span>()</span>
<span id="cb6-68"><a href="#cb6-68" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb6-69"><a href="#cb6-69" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-70"><a href="#cb6-70" aria-hidden="true" tabindex="-1"></a>carpinteria_plot</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell quarto-layout-panel">
<div class="quarto-layout-row quarto-layout-valign-top">
<div class="cell-output-display quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid" width="672"></p>
</div>
<div class="cell-output-display quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="index_files/figure-html/unnamed-chunk-6-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
</div>
</section>
</section>
<section id="stage-2-write-functions-to-clean-plot-your-data" class="level1">
<h1><strong><svg aria-label="Person running" role="img" viewbox="0 0 448 512" style="height:1em;width:0.88em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>Person running</title><path d="M336 48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM141.7 175.5c9.9-9.9 23.4-15.5 37.5-15.5c1.9 0 3.8 .1 5.6 .3L153.6 254c-9.3 28 1.7 58.8 26.8 74.5l86.2 53.9-25.4 88.8c-4.9 17 5 34.7 22 39.6s34.7-5 39.6-22l28.7-100.4c5.9-20.6-2.6-42.6-20.7-53.9L254 299l30.9-82.4 5.1 12.3C305 264.7 339.9 288 378.7 288H400c17.7 0 32-14.3 32-32s-14.3-32-32-32H378.7c-12.9 0-24.6-7.8-29.5-19.7l-6.3-15c-14.6-35.1-44.1-61.9-80.5-73.1l-48.7-15c-11.1-3.4-22.7-5.2-34.4-5.2c-31 0-60.8 12.3-82.7 34.3L73.4 153.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0l23.1-23.1zM107.2 352H48c-17.7 0-32 14.3-32 32s14.3 32 32 32h69.6c19 0 36.2-11.2 43.9-28.5L173 361.6l-9.5-6c-17.5-10.9-30.5-26.8-37.9-44.9L107.2 352z"></path></svg> Stage 2:</strong> Write functions to clean & plot your data</h1>
<p>Though there isn’t anything inherently <em>wrong</em> with copying/pasting large chunks of code, it’s a better practice to turn repeated code into functions. In Stage 2, we’ll turn our cleaning and plotting pipelines into functions that can be called on any of our similarly-structured data sets.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Where do I write/save my functions?
</div>
</div>
<div class="callout-body-container callout-body">
<p>While there’s no hard and fast rule, I tend to create a subdirectory within my project (e.g. named <code>/R</code>, <code>/utils</code>, etc.) to house all my function scripts. I prefer to create a separate <code>.R</code> script for each of my functions, and name each file the same as the function itself (e.g. if I’m writing a function called <code>do_fun_thing()</code>, I’d save it to a script called <code>do_fun_thing.R</code>).</p>
<p>You can then <strong>source</strong> your function files into whatever script (or <code>.rmd</code>/<code>.qmd</code> file) where you call that function (e.g. <code>source("utils/do_fun_thing.R")</code>.</p>
<p><strong>Note:</strong> You <em>cannot</em> <code>source()</code> a <code>.rmd</code> or <code>.qmd</code> file into another file/script, therefore it’s important to save functions to a <code>.R</code> file.</p>
<p>Here, let’s create the following:</p>
<ul>
<li>a <code>/utils</code> folder to store our functions scripts</li>
<li>a <code>clean_ocean_temps.R</code> file (saved to <code>/utils</code>), where we’ll write a function to clean our data</li>
<li>a <code>plot_ocean_temps.R</code> file (saved to <code>/utils</code>), where we’ll write a function to plot our data</li>
<li>a <code>functions_pipeline.R</code> file (saved to the root directory), where we’ll use our functions to clean and plot our data – you can also read-in your data files here, just as we did in <a href="http://localhost:7074/#ii.-import-raw-data">Stage 1, part ii</a></li>
</ul>
</div>
</div>
<section id="i.-write-a-function-to-clean-data-sets" class="level2">
<h2 class="anchored" data-anchor-id="i.-write-a-function-to-clean-data-sets"><strong>i.</strong> Write a function to <strong>clean</strong> data sets</h2>
<p>It’s helpful to first identify which parts of the cleaning code need to be generalized/made “flexible” so that any of our three data frames can be passed to it for cleaning. For us, that’s the <strong>name of the raw data frame</strong> and the <strong>site name character string</strong> that’s repeated for the length of the added <code>site</code> column (see annotation notes below the rendered code):</p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-8"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-8" data-target-annotation="1" onclick="event.preventDefault();" href="">1</a><span id="annotated-cell-8-1" class="code-annotation-target"><a href="#annotated-cell-8-1" aria-hidden="true" tabindex="-1"></a>alegria_clean <span class="ot"><-</span> alegria <span class="sc">|></span></span>
<span id="annotated-cell-8-2"><a href="#annotated-cell-8-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(year, month, day, decimal_time, Temp_top, Temp_mid, Temp_bot) <span class="sc">|></span></span>
<span id="annotated-cell-8-3"><a href="#annotated-cell-8-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(year <span class="sc">%in%</span> <span class="fu">c</span>(<span class="dv">2005</span><span class="sc">:</span><span class="dv">2020</span>)) <span class="sc">|></span> </span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-8" data-target-annotation="2" onclick="event.preventDefault();" href="">2</a><span id="annotated-cell-8-4" class="code-annotation-target"><a href="#annotated-cell-8-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">site =</span> <span class="fu">rep</span>(<span class="st">"Alegria Reef"</span>)) <span class="sc">|></span></span>
<span id="annotated-cell-8-5"><a href="#annotated-cell-8-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date, year, month, day, <span class="at">sep =</span> <span class="st">"-"</span>, <span class="at">remove =</span> <span class="cn">FALSE</span>) <span class="sc">|></span> </span>
<span id="annotated-cell-8-6"><a href="#annotated-cell-8-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">time =</span> <span class="fu">times</span>(decimal_time)) <span class="sc">|></span> </span>
<span id="annotated-cell-8-7"><a href="#annotated-cell-8-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date_time, date, time, <span class="at">sep =</span> <span class="st">" "</span>, <span class="at">remove =</span> <span class="cn">TRUE</span>) <span class="sc">|></span> </span>
<span id="annotated-cell-8-8"><a href="#annotated-cell-8-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">date_time =</span> <span class="fu">as.POSIXct</span>(date_time, <span class="st">"%Y-%m-%d %H:%M:%S"</span>, <span class="at">tz =</span> <span class="st">"GMT"</span>),</span>
<span id="annotated-cell-8-9"><a href="#annotated-cell-8-9" aria-hidden="true" tabindex="-1"></a> <span class="at">year =</span> <span class="fu">as.factor</span>(year),</span>
<span id="annotated-cell-8-10"><a href="#annotated-cell-8-10" aria-hidden="true" tabindex="-1"></a> <span class="at">month =</span> <span class="fu">as.factor</span>(month),</span>
<span id="annotated-cell-8-11"><a href="#annotated-cell-8-11" aria-hidden="true" tabindex="-1"></a> <span class="at">day =</span> <span class="fu">as.numeric</span>(day)) <span class="sc">|></span></span>
<span id="annotated-cell-8-12"><a href="#annotated-cell-8-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">month_name =</span> <span class="fu">as.factor</span>(month.name[month])) <span class="sc">|></span></span>
<span id="annotated-cell-8-13"><a href="#annotated-cell-8-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">replace_with_na</span>(<span class="at">replace =</span> <span class="fu">list</span>(<span class="at">Temp_bot =</span> <span class="dv">9999</span>, <span class="at">Temp_top =</span> <span class="dv">9999</span>, <span class="at">Temp_mid =</span> <span class="dv">9999</span>)) <span class="sc">|></span> </span>
<span id="annotated-cell-8-14"><a href="#annotated-cell-8-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(site, date_time, year, month, day, month_name, Temp_bot, Temp_mid, Temp_top)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-8" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="1" data-code-annotation="1" data-code-cell="annotated-cell-8">the name of the raw data frame (here, <code>alegria</code>) needs to be generalized</span>
</dd>
<dt data-target-cell="annotated-cell-8" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="4" data-code-annotation="2" data-code-cell="annotated-cell-8">the site name character string that’s repeated for the length of the added <code>site</code> column (here, <strong>“Alegria Reef”</strong>) needs to be generalized</span>
</dd>
</dl>
</div>
</div>
<p>Now we can start to build out our function. We’ll start by creating a super basic function, then build in more complexity. I encourage you to test out your function after each version to ensure that it works as you intend it to.</p>
<section id="version-1" class="level3">
<h3 class="anchored" data-anchor-id="version-1"><strong>Version 1:</strong></h3>
<p><strong><em>The primary goal of <code>clean_ocean_temps()</code> v1 is to get a basic data cleaning function working.</em></strong></p>
<p>To start, let’s create the skeleton of our function, which we’ll call <code>clean_ocean_temps()</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>clean_ocean_temps <span class="ot"><-</span> <span class="cf">function</span>(){</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb7-3"><a href="#cb7-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>
<p>Now, let’s copy and paste our cleaning code for Alegria Reef data (from <strong>Stage 1</strong>, above) into the body of the function (i.e. within the curly brackets, <code>{}</code>).</p>
<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>clean_ocean_temps <span class="ot"><-</span> <span class="cf">function</span>(){</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> alegria_clean <span class="ot"><-</span> alegria <span class="sc">|></span> </span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(year, month, day, decimal_time, Temp_top, Temp_mid, Temp_bot) <span class="sc">|></span></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(year <span class="sc">%in%</span> <span class="fu">c</span>(<span class="dv">2005</span><span class="sc">:</span><span class="dv">2020</span>)) <span class="sc">|></span> </span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">site =</span> <span class="fu">rep</span>(<span class="st">"Alegria Reef"</span>)) <span class="sc">|></span> </span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date, year, month, day, <span class="at">sep =</span> <span class="st">"-"</span>, <span class="at">remove =</span> <span class="cn">FALSE</span>) <span class="sc">|></span> </span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">time =</span> <span class="fu">times</span>(decimal_time)) <span class="sc">|></span> </span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date_time, date, time, <span class="at">sep =</span> <span class="st">" "</span>, <span class="at">remove =</span> <span class="cn">TRUE</span>) <span class="sc">|></span> </span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">date_time =</span> <span class="fu">as.POSIXct</span>(date_time, <span class="st">"%Y-%m-%d %H:%M:%S"</span>, <span class="at">tz =</span> <span class="st">"GMT"</span>),</span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a> <span class="at">year =</span> <span class="fu">as.factor</span>(year),</span>
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a> <span class="at">month =</span> <span class="fu">as.factor</span>(month),</span>
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a> <span class="at">day =</span> <span class="fu">as.numeric</span>(day)) <span class="sc">|></span></span>
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">month_name =</span> <span class="fu">as.factor</span>(month.name[month])) <span class="sc">|></span></span>
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">replace_with_na</span>(<span class="at">replace =</span> <span class="fu">list</span>(<span class="at">Temp_bot =</span> <span class="dv">9999</span>, <span class="at">Temp_top =</span> <span class="dv">9999</span>, <span class="at">Temp_mid =</span> <span class="dv">9999</span>)) <span class="sc">|></span> </span>
<span id="cb8-16"><a href="#cb8-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(site, date_time, year, month, day, month_name, Temp_bot, Temp_mid, Temp_top)</span>
<span id="cb8-17"><a href="#cb8-17" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb8-18"><a href="#cb8-18" 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>Next, we want the ability to provide our function with any three of our data sets for processing. To do so, we’ll make the following updates:</p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-11"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-11" data-target-annotation="1" onclick="event.preventDefault();" href="">1</a><span id="annotated-cell-11-1" class="code-annotation-target"><a href="#annotated-cell-11-1" aria-hidden="true" tabindex="-1"></a>clean_ocean_temps <span class="ot"><-</span> <span class="cf">function</span>(raw_data){</span>
<span id="annotated-cell-11-2"><a href="#annotated-cell-11-2" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-11-3"><a href="#annotated-cell-11-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># clean data ----</span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-11" data-target-annotation="2" onclick="event.preventDefault();" href="">2</a><span id="annotated-cell-11-4" class="code-annotation-target"><a href="#annotated-cell-11-4" aria-hidden="true" tabindex="-1"></a> temps_clean <span class="ot"><-</span> raw_data <span class="sc">|></span></span>
<span id="annotated-cell-11-5"><a href="#annotated-cell-11-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(year, month, day, decimal_time, Temp_top, Temp_mid, Temp_bot) <span class="sc">|></span></span>
<span id="annotated-cell-11-6"><a href="#annotated-cell-11-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(year <span class="sc">%in%</span> <span class="fu">c</span>(<span class="dv">2005</span><span class="sc">:</span><span class="dv">2020</span>)) <span class="sc">|></span> </span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-11" data-target-annotation="4" onclick="event.preventDefault();" href="">4</a><span id="annotated-cell-11-7" class="code-annotation-target"><a href="#annotated-cell-11-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">site =</span> <span class="fu">rep</span>(<span class="st">"_____"</span>)) <span class="sc">|></span></span>
<span id="annotated-cell-11-8"><a href="#annotated-cell-11-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date, year, month, day, <span class="at">sep =</span> <span class="st">"-"</span>, <span class="at">remove =</span> <span class="cn">FALSE</span>) <span class="sc">|></span> </span>
<span id="annotated-cell-11-9"><a href="#annotated-cell-11-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">time =</span> <span class="fu">times</span>(decimal_time)) <span class="sc">|></span> </span>
<span id="annotated-cell-11-10"><a href="#annotated-cell-11-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date_time, date, time, <span class="at">sep =</span> <span class="st">" "</span>, <span class="at">remove =</span> <span class="cn">TRUE</span>) <span class="sc">|></span> </span>
<span id="annotated-cell-11-11"><a href="#annotated-cell-11-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">date_time =</span> <span class="fu">as.POSIXct</span>(date_time, <span class="st">"%Y-%m-%d %H:%M:%S"</span>, <span class="at">tz =</span> <span class="st">"GMT"</span>),</span>
<span id="annotated-cell-11-12"><a href="#annotated-cell-11-12" aria-hidden="true" tabindex="-1"></a> <span class="at">year =</span> <span class="fu">as.factor</span>(year),</span>
<span id="annotated-cell-11-13"><a href="#annotated-cell-11-13" aria-hidden="true" tabindex="-1"></a> <span class="at">month =</span> <span class="fu">as.factor</span>(month),</span>
<span id="annotated-cell-11-14"><a href="#annotated-cell-11-14" aria-hidden="true" tabindex="-1"></a> <span class="at">day =</span> <span class="fu">as.numeric</span>(day)) <span class="sc">|></span></span>
<span id="annotated-cell-11-15"><a href="#annotated-cell-11-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">month_name =</span> <span class="fu">as.factor</span>(month.name[month])) <span class="sc">|></span></span>
<span id="annotated-cell-11-16"><a href="#annotated-cell-11-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">replace_with_na</span>(<span class="at">replace =</span> <span class="fu">list</span>(<span class="at">Temp_bot =</span> <span class="dv">9999</span>, <span class="at">Temp_top =</span> <span class="dv">9999</span>, <span class="at">Temp_mid =</span> <span class="dv">9999</span>)) <span class="sc">|></span> </span>
<span id="annotated-cell-11-17"><a href="#annotated-cell-11-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(site, date_time, year, month, day, month_name, Temp_bot, Temp_mid, Temp_top)</span>
<span id="annotated-cell-11-18"><a href="#annotated-cell-11-18" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-11-19"><a href="#annotated-cell-11-19" aria-hidden="true" tabindex="-1"></a> <span class="co"># return cleaned df ----</span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-11" data-target-annotation="3" onclick="event.preventDefault();" href="">3</a><span id="annotated-cell-11-20" class="code-annotation-target"><a href="#annotated-cell-11-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(temps_clean)</span>
<span id="annotated-cell-11-21"><a href="#annotated-cell-11-21" aria-hidden="true" tabindex="-1"></a>}</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-11" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="1" data-code-annotation="1" data-code-cell="annotated-cell-11">create an input (aka <strong>argument</strong>) called <code>raw_data</code> inside <code>function()</code> (NOTE: you can name your argument however you’d like, but preferably something short and descriptive)</span>
</dd>
<dt data-target-cell="annotated-cell-11" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="4" data-code-annotation="2" data-code-cell="annotated-cell-11">replace our hard-coded data frame name (e.g. <code>alegria</code> in the previous code chunk) in our cleaning pipeline with <code>raw_data</code></span>
</dd>
<dt data-target-cell="annotated-cell-11" data-target-annotation="3">3</dt>
<dd>
<span data-code-lines="20" data-code-annotation="3" data-code-cell="annotated-cell-11">update the name of the object we save our clean data to (currently <code>alegria_clean</code>) to something a bit more generalized, like <code>temps_clean</code>, and <code>return()</code> our clean data frame object at the end of the function</span>
</dd>
<dt data-target-cell="annotated-cell-11" data-target-annotation="4">4</dt>
<dd>
<span data-code-lines="7" data-code-annotation="4" data-code-cell="annotated-cell-11">recall that part of our cleaning pipeline includes adding a column called <code>site</code>, with repeating values that are the site name; for now, let’s just add some placeholder text (“_____”) and we’ll figure out how to make that text match up with the data in the next versions of our function</span>
</dd>
</dl>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
What is <code>return()</code> and when is it necessary?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-4" class="callout-4-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Oftentimes, we want a function to do some processing on whatever we provide it and then give us back the result. We use the <code>return()</code> function to do this in R.</p>
<p>R <strong>automatically</strong> returns the the last output of a function – here, it isn’t necessary to <code>return(temps_clean)</code> since the <code>temps_clean</code> data frame is the last output of our <code>clean_ocean_temps()</code> function.</p>
<p>An explicit <code>return()</code> is used to return a value immediately from a function. If it is <em>not</em> the last statement of a function, <code>return()</code> will prematurely end the function – for example, if <code>x = 2</code>, the string, <code>"Positive"</code> will be returned (and the remaining <code>if else</code> statement will not be executed):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>check_number <span class="ot"><-</span> <span class="cf">function</span>(x) {</span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (x <span class="sc">></span> <span class="dv">0</span>) {</span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(<span class="st">"Positive"</span>)</span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span> <span class="cf">if</span> (x <span class="sc"><</span> <span class="dv">0</span>) {</span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(<span class="st">"Negative"</span>)</span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span> {</span>
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(<span class="st">"Zero"</span>)</span>
<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb9-11"><a href="#cb9-11" 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>I tend to include an explicit <code>return()</code> at the end of my functions because I think it makes it easier to read/understand the code, but check out <a href="https://stackoverflow.com/questions/11738823/explicitly-calling-return-in-a-function-or-not">this interesting dialogue</a> on whether this is best practice or not.</p>
</div>
</div>
</div>
<p>Lastly, let’s make sure our function works. You can take a few approaches to trying out your work:</p>
<ol type="1">
<li><strong>test in the console:</strong> I do this a lot! It’s fast and allows you to test out different things if you encounter any sticking points. You’ll see me doing that while teaching</li>
<li><strong>write and run your <code>functions_pipeline.R</code> script:</strong> Begin writing your analysis script (here, that’s <code>functions_pipeline.R</code>) – below is one way you might consider setting up your script</li>
</ol>
<p>Regardless, make sure to always rerun/re-source your function after making changes so that an updated version gets saved to your global environment!</p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-12"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-12-1"><a href="#annotated-cell-12-1" aria-hidden="true" tabindex="-1"></a><span class="co"># functions_pipeline.R #</span></span>
<span id="annotated-cell-12-2"><a href="#annotated-cell-12-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-12-3"><a href="#annotated-cell-12-3" aria-hidden="true" tabindex="-1"></a><span class="co">#..........................load packages......................... </span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-12" data-target-annotation="1" onclick="event.preventDefault();" href="">1</a><span id="annotated-cell-12-4" class="code-annotation-target"><a href="#annotated-cell-12-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(readr)</span>
<span id="annotated-cell-12-5"><a href="#annotated-cell-12-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span>
<span id="annotated-cell-12-6"><a href="#annotated-cell-12-6" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyr)</span>
<span id="annotated-cell-12-7"><a href="#annotated-cell-12-7" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(chron)</span>
<span id="annotated-cell-12-8"><a href="#annotated-cell-12-8" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(naniar)</span>
<span id="annotated-cell-12-9"><a href="#annotated-cell-12-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-12-10"><a href="#annotated-cell-12-10" aria-hidden="true" tabindex="-1"></a><span class="co">#........................source functions........................ </span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-12" data-target-annotation="2" onclick="event.preventDefault();" href="">2</a><span id="annotated-cell-12-11" class="code-annotation-target"><a href="#annotated-cell-12-11" aria-hidden="true" tabindex="-1"></a><span class="fu">source</span>(<span class="st">"utils/clean_ocean_temps.R"</span>)</span>
<span id="annotated-cell-12-12"><a href="#annotated-cell-12-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-12-13"><a href="#annotated-cell-12-13" aria-hidden="true" tabindex="-1"></a><span class="co">#..........................import data........................... </span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-12" data-target-annotation="3" onclick="event.preventDefault();" href="">3</a><span id="annotated-cell-12-14" class="code-annotation-target"><a href="#annotated-cell-12-14" aria-hidden="true" tabindex="-1"></a>alegria <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"alegria_mooring_ale_20210617.csv"</span>))</span>
<span id="annotated-cell-12-15"><a href="#annotated-cell-12-15" aria-hidden="true" tabindex="-1"></a>mohawk <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"mohawk_mooring_mko_20220330.csv"</span>))</span>
<span id="annotated-cell-12-16"><a href="#annotated-cell-12-16" aria-hidden="true" tabindex="-1"></a>carpinteria <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"carpinteria_mooring_car_20220330.csv"</span>))</span>
<span id="annotated-cell-12-17"><a href="#annotated-cell-12-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-12-18"><a href="#annotated-cell-12-18" aria-hidden="true" tabindex="-1"></a><span class="co">#...........................clean data........................... </span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-12" data-target-annotation="4" onclick="event.preventDefault();" href="">4</a><span id="annotated-cell-12-19" class="code-annotation-target"><a href="#annotated-cell-12-19" aria-hidden="true" tabindex="-1"></a>alegria_clean <span class="ot"><-</span> <span class="fu">clean_ocean_temps</span>(<span class="at">raw_data =</span> alegria)</span>
<span id="annotated-cell-12-20"><a href="#annotated-cell-12-20" aria-hidden="true" tabindex="-1"></a>mohawk_clean <span class="ot"><-</span> <span class="fu">clean_ocean_temps</span>(<span class="at">raw_data =</span> mohawk)</span>
<span id="annotated-cell-12-21"><a href="#annotated-cell-12-21" aria-hidden="true" tabindex="-1"></a>carpinteria_clean <span class="ot"><-</span> <span class="fu">clean_ocean_temps</span>(<span class="at">raw_data =</span> carpinteria)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-12" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="4,5,6,7,8" data-code-annotation="1" data-code-cell="annotated-cell-12">make sure any packages that your function relies on are installed/imported <a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a></span>
</dd>
<dt data-target-cell="annotated-cell-12" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="11" data-code-annotation="2" data-code-cell="annotated-cell-12">source your function into your script</span>
</dd>
<dt data-target-cell="annotated-cell-12" data-target-annotation="3">3</dt>
<dd>
<span data-code-lines="14,15,16" data-code-annotation="3" data-code-cell="annotated-cell-12">read in your data</span>
</dd>
<dt data-target-cell="annotated-cell-12" data-target-annotation="4">4</dt>
<dd>
<span data-code-lines="19,20,21" data-code-annotation="4" data-code-cell="annotated-cell-12">use your <code>clean_ocean_temps()</code> function to process your raw data</span>
</dd>
</dl>
</div>
</div>
</section>
<section id="version-2" class="level3">
<h3 class="anchored" data-anchor-id="version-2"><strong>Version 2:</strong></h3>
<p><strong><em>The primary goal of <code>clean_ocean_temps() v2</code> is to create a <code>site</code> column that contains the correct site name.</em></strong></p>
<p>There are lots of creative ways to go about forming the site name that will get added as a repeating value to the <code>site</code> column, but the easiest and most explicit is likely adding a second function argument that takes the site name as a character string (e.g. <code>site_name = "alegria"</code>)</p>
<p>Let’s add that to our function:</p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-13"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-13" data-target-annotation="1" onclick="event.preventDefault();" href="">1</a><span id="annotated-cell-13-1" class="code-annotation-target"><a href="#annotated-cell-13-1" aria-hidden="true" tabindex="-1"></a>clean_ocean_temps <span class="ot"><-</span> <span class="cf">function</span>(raw_data, site_name){</span>
<span id="annotated-cell-13-2"><a href="#annotated-cell-13-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-13-3"><a href="#annotated-cell-13-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># format `site_name` ----</span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-13" data-target-annotation="2" onclick="event.preventDefault();" href="">2</a><span id="annotated-cell-13-4" class="code-annotation-target"><a href="#annotated-cell-13-4" aria-hidden="true" tabindex="-1"></a> site_name_formatted <span class="ot"><-</span> <span class="fu">paste</span>(<span class="fu">str_to_title</span>(site_name), <span class="st">"Reef"</span>)</span>
<span id="annotated-cell-13-5"><a href="#annotated-cell-13-5" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-13-6"><a href="#annotated-cell-13-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># clean data ----</span></span>
<span id="annotated-cell-13-7"><a href="#annotated-cell-13-7" aria-hidden="true" tabindex="-1"></a> temps_clean <span class="ot"><-</span> raw_data <span class="sc">|></span> </span>
<span id="annotated-cell-13-8"><a href="#annotated-cell-13-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(year, month, day, decimal_time, Temp_top, Temp_mid, Temp_bot) <span class="sc">|></span></span>
<span id="annotated-cell-13-9"><a href="#annotated-cell-13-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(year <span class="sc">%in%</span> <span class="fu">c</span>(<span class="dv">2005</span><span class="sc">:</span><span class="dv">2020</span>)) <span class="sc">|></span> </span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-13" data-target-annotation="3" onclick="event.preventDefault();" href="">3</a><span id="annotated-cell-13-10" class="code-annotation-target"><a href="#annotated-cell-13-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">site =</span> <span class="fu">rep</span>(site_name_formatted)) <span class="sc">|></span></span>
<span id="annotated-cell-13-11"><a href="#annotated-cell-13-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date, year, month, day, <span class="at">sep =</span> <span class="st">"-"</span>, <span class="at">remove =</span> <span class="cn">FALSE</span>) <span class="sc">|></span> </span>
<span id="annotated-cell-13-12"><a href="#annotated-cell-13-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">time =</span> <span class="fu">times</span>(decimal_time)) <span class="sc">|></span> </span>
<span id="annotated-cell-13-13"><a href="#annotated-cell-13-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date_time, date, time, <span class="at">sep =</span> <span class="st">" "</span>, <span class="at">remove =</span> <span class="cn">TRUE</span>) <span class="sc">|></span> </span>
<span id="annotated-cell-13-14"><a href="#annotated-cell-13-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">date_time =</span> <span class="fu">as.POSIXct</span>(date_time, <span class="st">"%Y-%m-%d %H:%M:%S"</span>, <span class="at">tz =</span> <span class="st">"GMT"</span>),</span>
<span id="annotated-cell-13-15"><a href="#annotated-cell-13-15" aria-hidden="true" tabindex="-1"></a> <span class="at">year =</span> <span class="fu">as.factor</span>(year),</span>
<span id="annotated-cell-13-16"><a href="#annotated-cell-13-16" aria-hidden="true" tabindex="-1"></a> <span class="at">month =</span> <span class="fu">as.factor</span>(month),</span>
<span id="annotated-cell-13-17"><a href="#annotated-cell-13-17" aria-hidden="true" tabindex="-1"></a> <span class="at">day =</span> <span class="fu">as.numeric</span>(day)) <span class="sc">|></span></span>
<span id="annotated-cell-13-18"><a href="#annotated-cell-13-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">month_name =</span> <span class="fu">as.factor</span>(month.name[month])) <span class="sc">|></span></span>
<span id="annotated-cell-13-19"><a href="#annotated-cell-13-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">replace_with_na</span>(<span class="at">replace =</span> <span class="fu">list</span>(<span class="at">Temp_bot =</span> <span class="dv">9999</span>, <span class="at">Temp_top =</span> <span class="dv">9999</span>, <span class="at">Temp_mid =</span> <span class="dv">9999</span>)) <span class="sc">|></span> </span>
<span id="annotated-cell-13-20"><a href="#annotated-cell-13-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(site, date_time, year, month, day, month_name, Temp_bot, Temp_mid, Temp_top)</span>
<span id="annotated-cell-13-21"><a href="#annotated-cell-13-21" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-13-22"><a href="#annotated-cell-13-22" aria-hidden="true" tabindex="-1"></a> <span class="co"># return cleaned df ----</span></span>
<span id="annotated-cell-13-23"><a href="#annotated-cell-13-23" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(temps_clean) </span>
<span id="annotated-cell-13-24"><a href="#annotated-cell-13-24" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-13-25"><a href="#annotated-cell-13-25" aria-hidden="true" tabindex="-1"></a>}</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-13" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="1" data-code-annotation="1" data-code-cell="annotated-cell-13">add a second argument called <code>site_name</code> (or something intuitive)</span>
</dd>
<dt data-target-cell="annotated-cell-13" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="4" data-code-annotation="2" data-code-cell="annotated-cell-13">we might specify in our function documentation that <code>site_name</code> takes the standard site name (as used by SBC LTER) as a character string, but what happens if a users uses different character casing than expected (e.g. <code>"alegria"</code>, <code>"MOHAWK"</code>, <code>"Carpinteria"</code>)? We can use a combination of <code>paste()</code> and the <code>{stringr}</code> package to format that value as we’d like it to appear in our <code>site</code> column – here, that’s converting it to Title Case and pasting <code>"Reef"</code> at the end (e.g. <code>"alegria"</code> will become <code>"Alegria Reef"</code>) ; <svg aria-label="Book open reader" role="img" viewbox="0 0 512 512" style="height:1em;width:1em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>Book open reader</title><path d="M352 96c0 53-43 96-96 96s-96-43-96-96s43-96 96-96s96 43 96 96zM240 248V512l-48.4-24.2c-20.9-10.4-43.5-17-66.8-19.3l-96-9.6C12.5 457.2 0 443.5 0 427V224c0-17.7 14.3-32 32-32H62.3c63.6 0 125.6 19.6 177.7 56zm32 264V248c52.1-36.4 114.1-56 177.7-56H480c17.7 0 32 14.3 32 32V427c0 16.4-12.5 30.2-28.8 31.8l-96 9.6c-23.2 2.3-45.9 8.9-66.8 19.3L272 512z"></path></svg> I referenced <a href="https://stringr.tidyverse.org/reference/case.html">this resource</a>)</span>
</dd>
<dt data-target-cell="annotated-cell-13" data-target-annotation="3">3</dt>
<dd>
<span data-code-lines="10" data-code-annotation="3" data-code-cell="annotated-cell-13">substitute our formatted character string, <code>site_name_formatted</code>, in for the hard-coded site name in <code>mutate(site = rep("___ Reef"))</code></span>
</dd>
</dl>
</div>
</div>
<p>Rerun the updated function and try using it to make sure the appropriate site name is added to the <code>site_name</code> column for each of the data sets:</p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-14"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-14-1"><a href="#annotated-cell-14-1" aria-hidden="true" tabindex="-1"></a><span class="co"># functions_pipeline.R #</span></span>
<span id="annotated-cell-14-2"><a href="#annotated-cell-14-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-14-3"><a href="#annotated-cell-14-3" aria-hidden="true" tabindex="-1"></a><span class="co">#..........................load packages......................... </span></span>
<span id="annotated-cell-14-4"><a href="#annotated-cell-14-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(readr)</span>
<span id="annotated-cell-14-5"><a href="#annotated-cell-14-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr) </span>
<span id="annotated-cell-14-6"><a href="#annotated-cell-14-6" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyr)</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-14" data-target-annotation="1" onclick="event.preventDefault();" href="">1</a><span id="annotated-cell-14-7" class="code-annotation-target"><a href="#annotated-cell-14-7" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(stringr)</span>
<span id="annotated-cell-14-8"><a href="#annotated-cell-14-8" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(chron)</span>
<span id="annotated-cell-14-9"><a href="#annotated-cell-14-9" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(naniar)</span>
<span id="annotated-cell-14-10"><a href="#annotated-cell-14-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-14-11"><a href="#annotated-cell-14-11" aria-hidden="true" tabindex="-1"></a><span class="co">#........................source functions........................ </span></span>
<span id="annotated-cell-14-12"><a href="#annotated-cell-14-12" aria-hidden="true" tabindex="-1"></a><span class="fu">source</span>(<span class="st">"utils/clean_ocean_temps.R"</span>) </span>
<span id="annotated-cell-14-13"><a href="#annotated-cell-14-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-14-14"><a href="#annotated-cell-14-14" aria-hidden="true" tabindex="-1"></a><span class="co">#..........................import data........................... </span></span>
<span id="annotated-cell-14-15"><a href="#annotated-cell-14-15" aria-hidden="true" tabindex="-1"></a>alegria <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"alegria_mooring_ale_20210617.csv"</span>))</span>
<span id="annotated-cell-14-16"><a href="#annotated-cell-14-16" aria-hidden="true" tabindex="-1"></a>mohawk <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"mohawk_mooring_mko_20220330.csv"</span>))</span>
<span id="annotated-cell-14-17"><a href="#annotated-cell-14-17" aria-hidden="true" tabindex="-1"></a>carpinteria <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"carpinteria_mooring_car_20220330.csv"</span>))</span>
<span id="annotated-cell-14-18"><a href="#annotated-cell-14-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-14-19"><a href="#annotated-cell-14-19" aria-hidden="true" tabindex="-1"></a><span class="co">#...........................clean data........................... </span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-14" data-target-annotation="2" onclick="event.preventDefault();" href="">2</a><span id="annotated-cell-14-20" class="code-annotation-target"><a href="#annotated-cell-14-20" aria-hidden="true" tabindex="-1"></a>alegria_clean <span class="ot"><-</span> <span class="fu">clean_ocean_temps</span>(<span class="at">raw_data =</span> alegria, <span class="at">site_name =</span> <span class="st">"alegria"</span>)</span>
<span id="annotated-cell-14-21"><a href="#annotated-cell-14-21" aria-hidden="true" tabindex="-1"></a>mohawk_clean <span class="ot"><-</span> <span class="fu">clean_ocean_temps</span>(<span class="at">raw_data =</span> mohawk, <span class="at">site_name =</span> <span class="st">"MOHAWK"</span>)</span>
<span id="annotated-cell-14-22"><a href="#annotated-cell-14-22" aria-hidden="true" tabindex="-1"></a>carpinteria_clean <span class="ot"><-</span> <span class="fu">clean_ocean_temps</span>(<span class="at">raw_data =</span> carpinteria, <span class="at">site_name =</span> <span class="st">"Carpinteria"</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-14" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="7" data-code-annotation="1" data-code-cell="annotated-cell-14">remember to import the <code>{stringr}</code> package, since we’ll be using <code>stringr::replace_with_na()</code> within our function</span>
</dd>
<dt data-target-cell="annotated-cell-14" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="20,21,22" data-code-annotation="2" data-code-cell="annotated-cell-14">add in the <code>site_name</code> argument – here, we demonstrate that how our function handles formatting, regardless of character case</span>
</dd>
</dl>
</div>
</div>
</section>
<section id="version-3" class="level3">
<h3 class="anchored" data-anchor-id="version-3"><strong>Version 3:</strong></h3>
<p><strong><em>The primary goal of `clean_ocean_temps() v3 is to provide a way for users to select which temperature measurements (Temp_top, Temp_mid, Temp_bot) to include in the cleaned data frame.</em></strong></p>
<p>Our function works perfectly fine as-is, but let’s say we don’t always want all three temperature measurements (surface temperature (<code>Temp_top</code>), mid-column temperature (<code>Temp_mid</code>), and bottom temperature (<code>Temp_top</code>)) included in our cleaned data. We can build flexibility into our function by adding an argument that allows the user to select exactly which of the three temperature measurements to include in the resulting cleaned data frame. To do this, we’ll make the following changes:</p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-15"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-15" data-target-annotation="1" onclick="event.preventDefault();" href="">1</a><span id="annotated-cell-15-1" class="code-annotation-target"><a href="#annotated-cell-15-1" aria-hidden="true" tabindex="-1"></a>clean_ocean_temps <span class="ot"><-</span> <span class="cf">function</span>(raw_data, site_name, <span class="at">include_temps =</span> <span class="fu">c</span>(<span class="st">"Temp_top"</span>, <span class="st">"Temp_mid"</span>, <span class="st">"Temp_bot"</span>)){</span>
<span id="annotated-cell-15-2"><a href="#annotated-cell-15-2" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-15-3"><a href="#annotated-cell-15-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># format `site_name` ----</span></span>
<span id="annotated-cell-15-4"><a href="#annotated-cell-15-4" aria-hidden="true" tabindex="-1"></a> site_name_formatted <span class="ot"><-</span> <span class="fu">paste</span>(<span class="fu">str_to_title</span>(site_name), <span class="st">"Reef"</span>)</span>
<span id="annotated-cell-15-5"><a href="#annotated-cell-15-5" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-15-6"><a href="#annotated-cell-15-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># columns to select ---- </span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-15" data-target-annotation="2" onclick="event.preventDefault();" href="">2</a><span id="annotated-cell-15-7" class="code-annotation-target"><a href="#annotated-cell-15-7" aria-hidden="true" tabindex="-1"></a> always_selected_cols <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"year"</span>, <span class="st">"month"</span>, <span class="st">"day"</span>, <span class="st">"decimal_time"</span>)</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-15" data-target-annotation="3" onclick="event.preventDefault();" href="">3</a><span id="annotated-cell-15-8" class="code-annotation-target"><a href="#annotated-cell-15-8" aria-hidden="true" tabindex="-1"></a> all_cols <span class="ot"><-</span> <span class="fu">append</span>(always_selected_cols, include_temps)</span>
<span id="annotated-cell-15-9"><a href="#annotated-cell-15-9" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-15-10"><a href="#annotated-cell-15-10" aria-hidden="true" tabindex="-1"></a> <span class="co"># clean data ----</span></span>
<span id="annotated-cell-15-11"><a href="#annotated-cell-15-11" aria-hidden="true" tabindex="-1"></a> temps_clean <span class="ot"><-</span> raw_data <span class="sc">|></span> </span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-15" data-target-annotation="4" onclick="event.preventDefault();" href="">4</a><span id="annotated-cell-15-12" class="code-annotation-target"><a href="#annotated-cell-15-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="fu">all_of</span>(all_cols)) <span class="sc">|></span></span>
<span id="annotated-cell-15-13"><a href="#annotated-cell-15-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(year <span class="sc">%in%</span> <span class="fu">c</span>(<span class="dv">2005</span><span class="sc">:</span><span class="dv">2020</span>)) <span class="sc">|></span> </span>
<span id="annotated-cell-15-14"><a href="#annotated-cell-15-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">site =</span> <span class="fu">rep</span>(site_name_formatted)) <span class="sc">|></span> </span>
<span id="annotated-cell-15-15"><a href="#annotated-cell-15-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date, year, month, day, <span class="at">sep =</span> <span class="st">"-"</span>, <span class="at">remove =</span> <span class="cn">FALSE</span>) <span class="sc">|></span> </span>
<span id="annotated-cell-15-16"><a href="#annotated-cell-15-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">time =</span> <span class="fu">times</span>(decimal_time)) <span class="sc">|></span> </span>
<span id="annotated-cell-15-17"><a href="#annotated-cell-15-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date_time, date, time, <span class="at">sep =</span> <span class="st">" "</span>, <span class="at">remove =</span> <span class="cn">TRUE</span>) <span class="sc">|></span> </span>
<span id="annotated-cell-15-18"><a href="#annotated-cell-15-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">date_time =</span> <span class="fu">as.POSIXct</span>(date_time, <span class="st">"%Y-%m-%d %H:%M:%S"</span>, <span class="at">tz =</span> <span class="st">"GMT"</span>),</span>
<span id="annotated-cell-15-19"><a href="#annotated-cell-15-19" aria-hidden="true" tabindex="-1"></a> <span class="at">year =</span> <span class="fu">as.factor</span>(year),</span>
<span id="annotated-cell-15-20"><a href="#annotated-cell-15-20" aria-hidden="true" tabindex="-1"></a> <span class="at">month =</span> <span class="fu">as.factor</span>(month),</span>
<span id="annotated-cell-15-21"><a href="#annotated-cell-15-21" aria-hidden="true" tabindex="-1"></a> <span class="at">day =</span> <span class="fu">as.numeric</span>(day)) <span class="sc">|></span></span>
<span id="annotated-cell-15-22"><a href="#annotated-cell-15-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">month_name =</span> <span class="fu">as.factor</span>(month.name[month])) <span class="sc">|></span></span>
<span id="annotated-cell-15-23"><a href="#annotated-cell-15-23" aria-hidden="true" tabindex="-1"></a> <span class="fu">replace_with_na</span>(<span class="at">replace =</span> <span class="fu">list</span>(<span class="at">Temp_bot =</span> <span class="dv">9999</span>, <span class="at">Temp_top =</span> <span class="dv">9999</span>, <span class="at">Temp_mid =</span> <span class="dv">9999</span>)) <span class="sc">|></span> </span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-15" data-target-annotation="5" onclick="event.preventDefault();" href="">5</a><span id="annotated-cell-15-24" class="code-annotation-target"><a href="#annotated-cell-15-24" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="fu">any_of</span>(<span class="fu">c</span>(<span class="st">"site"</span>, <span class="st">"date_time"</span>, <span class="st">"year"</span>, <span class="st">"month"</span>, <span class="st">"day"</span>, <span class="st">"month_name"</span>, <span class="st">"Temp_bot"</span>, <span class="st">"Temp_mid"</span>, <span class="st">"Temp_top"</span>)))</span>
<span id="annotated-cell-15-25"><a href="#annotated-cell-15-25" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-15-26"><a href="#annotated-cell-15-26" aria-hidden="true" tabindex="-1"></a> <span class="co"># return cleaned df ----</span></span>
<span id="annotated-cell-15-27"><a href="#annotated-cell-15-27" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(temps_clean) </span>
<span id="annotated-cell-15-28"><a href="#annotated-cell-15-28" aria-hidden="true" tabindex="-1"></a>}</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-15" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="1" data-code-annotation="1" data-code-cell="annotated-cell-15">add a third argument called <code>include_temps</code>, which defaults to including all three temperature variables (<code>Temp_top</code>, <code>Temp_mid</code>, <code>Temp_bot</code>)</span>
</dd>
<dt data-target-cell="annotated-cell-15" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="7" data-code-annotation="2" data-code-cell="annotated-cell-15">create a vector of variable names that should <em>always</em> be selected</span>
</dd>
<dt data-target-cell="annotated-cell-15" data-target-annotation="3">3</dt>
<dd>
<span data-code-lines="8" data-code-annotation="3" data-code-cell="annotated-cell-15">combine the “always selected” variables with the user-selected variables as specified using the <code>include_temps</code> argument</span>
</dd>
<dt data-target-cell="annotated-cell-15" data-target-annotation="4">4</dt>
<dd>
<span data-code-lines="12" data-code-annotation="4" data-code-cell="annotated-cell-15"><code>select</code> columns based on variables names in our <code>all_cols</code> vector using <code>select(all_of())</code> (<svg aria-label="Book open reader" role="img" viewbox="0 0 512 512" style="height:1em;width:1em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>Book open reader</title><path d="M352 96c0 53-43 96-96 96s-96-43-96-96s43-96 96-96s96 43 96 96zM240 248V512l-48.4-24.2c-20.9-10.4-43.5-17-66.8-19.3l-96-9.6C12.5 457.2 0 443.5 0 427V224c0-17.7 14.3-32 32-32H62.3c63.6 0 125.6 19.6 177.7 56zm32 264V248c52.1-36.4 114.1-56 177.7-56H480c17.7 0 32 14.3 32 32V427c0 16.4-12.5 30.2-28.8 31.8l-96 9.6c-23.2 2.3-45.9 8.9-66.8 19.3L272 512z"></path></svg> I first got an error message <a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"><sup>2</sup></a> when attempting to <code>select(all_cols)</code>, since an external vector alone can’t be used to make selections; I then referenced this <a href="https://tidyselect.r-lib.org/reference/all_of.html">resource</a>)</span>
</dd>
<dt data-target-cell="annotated-cell-15" data-target-annotation="5">5</dt>
<dd>
<span data-code-lines="24" data-code-annotation="5" data-code-cell="annotated-cell-15">make our last <code>select</code> call <a href="#fn3" class="footnote-ref" id="fnref3" role="doc-noteref"><sup>3</sup></a>, which is used to reorder columns, flexible enough to reorder temperature variables which may or may not be present using <code>select(any_of())</code> (<svg aria-label="Book open reader" role="img" viewbox="0 0 512 512" style="height:1em;width:1em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:#5A5A5A;overflow:visible;position:relative;"><title>Book open reader</title><path d="M352 96c0 53-43 96-96 96s-96-43-96-96s43-96 96-96s96 43 96 96zM240 248V512l-48.4-24.2c-20.9-10.4-43.5-17-66.8-19.3l-96-9.6C12.5 457.2 0 443.5 0 427V224c0-17.7 14.3-32 32-32H62.3c63.6 0 125.6 19.6 177.7 56zm32 264V248c52.1-36.4 114.1-56 177.7-56H480c17.7 0 32 14.3 32 32V427c0 16.4-12.5 30.2-28.8 31.8l-96 9.6c-23.2 2.3-45.9 8.9-66.8 19.3L272 512z"></path></svg> I again referenced this <a href="https://tidyselect.r-lib.org/reference/all_of.html">resource</a>)</span>
</dd>
</dl>
</div>
</div>
<p>Rerun the updated function and try out our new <code>inlcude_temps</code> argument:</p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-16"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-16-1"><a href="#annotated-cell-16-1" aria-hidden="true" tabindex="-1"></a><span class="co"># functions_pipeline.R #</span></span>
<span id="annotated-cell-16-2"><a href="#annotated-cell-16-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-16-3"><a href="#annotated-cell-16-3" aria-hidden="true" tabindex="-1"></a><span class="co">#..........................load packages......................... </span></span>
<span id="annotated-cell-16-4"><a href="#annotated-cell-16-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(readr)</span>
<span id="annotated-cell-16-5"><a href="#annotated-cell-16-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr) </span>
<span id="annotated-cell-16-6"><a href="#annotated-cell-16-6" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyr)</span>
<span id="annotated-cell-16-7"><a href="#annotated-cell-16-7" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(stringr) </span>
<span id="annotated-cell-16-8"><a href="#annotated-cell-16-8" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(chron)</span>
<span id="annotated-cell-16-9"><a href="#annotated-cell-16-9" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(naniar)</span>
<span id="annotated-cell-16-10"><a href="#annotated-cell-16-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-16-11"><a href="#annotated-cell-16-11" aria-hidden="true" tabindex="-1"></a><span class="co">#........................source functions........................ </span></span>
<span id="annotated-cell-16-12"><a href="#annotated-cell-16-12" aria-hidden="true" tabindex="-1"></a><span class="fu">source</span>(<span class="st">"utils/clean_ocean_temps.R"</span>) </span>
<span id="annotated-cell-16-13"><a href="#annotated-cell-16-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-16-14"><a href="#annotated-cell-16-14" aria-hidden="true" tabindex="-1"></a><span class="co">#..........................import data........................... </span></span>
<span id="annotated-cell-16-15"><a href="#annotated-cell-16-15" aria-hidden="true" tabindex="-1"></a>alegria <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"alegria_mooring_ale_20210617.csv"</span>))</span>
<span id="annotated-cell-16-16"><a href="#annotated-cell-16-16" aria-hidden="true" tabindex="-1"></a>mohawk <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"mohawk_mooring_mko_20220330.csv"</span>))</span>
<span id="annotated-cell-16-17"><a href="#annotated-cell-16-17" aria-hidden="true" tabindex="-1"></a>carpinteria <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"carpinteria_mooring_car_20220330.csv"</span>))</span>
<span id="annotated-cell-16-18"><a href="#annotated-cell-16-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-16-19"><a href="#annotated-cell-16-19" aria-hidden="true" tabindex="-1"></a><span class="co">#...........................clean data........................... </span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-16" data-target-annotation="1" onclick="event.preventDefault();" href="">1</a><span id="annotated-cell-16-20" class="code-annotation-target"><a href="#annotated-cell-16-20" aria-hidden="true" tabindex="-1"></a>alegria_clean <span class="ot"><-</span> <span class="fu">clean_ocean_temps</span>(<span class="at">raw_data =</span> alegria, <span class="at">site_name =</span> <span class="st">"alegria"</span>, <span class="at">include_temps =</span> <span class="fu">c</span>(<span class="st">"Temp_bot"</span>)) <span class="co"># includes only `Temp_bot`</span></span>
<span id="annotated-cell-16-21"><a href="#annotated-cell-16-21" aria-hidden="true" tabindex="-1"></a>mohawk_clean <span class="ot"><-</span> <span class="fu">clean_ocean_temps</span>(<span class="at">raw_data =</span> mohawk, <span class="at">site_name =</span> <span class="st">"MOHAWK"</span>) <span class="co"># includes all three temp cols (`Temp_top`, `Temp_mid`, `Temp_bot`) by default</span></span>
<span id="annotated-cell-16-22"><a href="#annotated-cell-16-22" aria-hidden="true" tabindex="-1"></a>carpinteria_clean <span class="ot"><-</span> <span class="fu">clean_ocean_temps</span>(<span class="at">raw_data =</span> carpinteria, <span class="at">site_name =</span> <span class="st">"Carpinteria"</span>, <span class="at">include_temps =</span> <span class="fu">c</span>(<span class="st">"Temp_mid"</span>, <span class="st">"Temp_bot"</span>)) <span class="co"># includes only `Temp_mid` & `Temp_bot`</span></span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-16" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="20,21,22" data-code-annotation="1" data-code-cell="annotated-cell-16">try using our new <code>include_temps</code> argument to return a cleaned data frame with a subset of the temperature measurement variables</span>
</dd>
</dl>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<strong>Note:</strong> <code>naniar::replace_with_na()</code> may throw a warning message
</div>
</div>
<div class="callout-body-container callout-body">
<p>However this will <em>not</em> halt execution – if one or more of the temperature variables are missing (e.g. if we specify <code>include_temps = c("Temp_bot")</code>, you will get a warning that says, <code>Missing from data: Temp_top, Temp_mid</code>).</p>
</div>
</div>
</section>
<section id="version-4" class="level3">
<h3 class="anchored" data-anchor-id="version-4"><strong>Version 4:</strong></h3>
<p><strong><em>The primary goal of <code>clean_ocean_temps()</code> v4 is to build in checks to ensure that the data provided to the function is compatible with the cleaning pipeline (i.e. ensure that the correct columns are present.</em></strong></p>
<p>To wrap things up, we might consider adding an <code>if else</code> statement that checks to ensure that the data provided is suitable for our cleaning pipeline:</p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-17"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-17-1"><a href="#annotated-cell-17-1" aria-hidden="true" tabindex="-1"></a>clean_ocean_temps <span class="ot"><-</span> <span class="cf">function</span>(raw_data, site_name, <span class="at">include_temps =</span> <span class="fu">c</span>(<span class="st">"Temp_top"</span>, <span class="st">"Temp_mid"</span>, <span class="st">"Temp_bot"</span>)){ </span>
<span id="annotated-cell-17-2"><a href="#annotated-cell-17-2" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-17-3"><a href="#annotated-cell-17-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># if data contains these colnames, clean the script ----</span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-17" data-target-annotation="1" onclick="event.preventDefault();" href="">1</a><span id="annotated-cell-17-4" class="code-annotation-target"><a href="#annotated-cell-17-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">all</span>(<span class="fu">c</span>(<span class="st">"year"</span>, <span class="st">"month"</span>, <span class="st">"day"</span>, <span class="st">"decimal_time"</span>, <span class="st">"Temp_bot"</span>, <span class="st">"Temp_top"</span>, <span class="st">"Temp_mid"</span>) <span class="sc">%in%</span> <span class="fu">colnames</span>(raw_data))) {</span>
<span id="annotated-cell-17-5"><a href="#annotated-cell-17-5" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-17-6"><a href="#annotated-cell-17-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">message</span>(<span class="st">"Cleaning data..."</span>)</span>
<span id="annotated-cell-17-7"><a href="#annotated-cell-17-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-17-8"><a href="#annotated-cell-17-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># format `site_name` ----</span></span>
<span id="annotated-cell-17-9"><a href="#annotated-cell-17-9" aria-hidden="true" tabindex="-1"></a> site_name_formatted <span class="ot"><-</span> <span class="fu">paste</span>(<span class="fu">str_to_title</span>(site_name), <span class="st">"Reef"</span>)</span>
<span id="annotated-cell-17-10"><a href="#annotated-cell-17-10" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-17-11"><a href="#annotated-cell-17-11" aria-hidden="true" tabindex="-1"></a> <span class="co"># columns to select ----</span></span>
<span id="annotated-cell-17-12"><a href="#annotated-cell-17-12" aria-hidden="true" tabindex="-1"></a> standard_cols <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"year"</span>, <span class="st">"month"</span>, <span class="st">"day"</span>, <span class="st">"decimal_time"</span>) </span>
<span id="annotated-cell-17-13"><a href="#annotated-cell-17-13" aria-hidden="true" tabindex="-1"></a> all_cols <span class="ot"><-</span> <span class="fu">append</span>(standard_cols, include_temps) </span>
<span id="annotated-cell-17-14"><a href="#annotated-cell-17-14" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-17-15"><a href="#annotated-cell-17-15" aria-hidden="true" tabindex="-1"></a> <span class="co"># clean data ----</span></span>
<span id="annotated-cell-17-16"><a href="#annotated-cell-17-16" aria-hidden="true" tabindex="-1"></a> temps_clean <span class="ot"><-</span> raw_data <span class="sc">|></span> </span>
<span id="annotated-cell-17-17"><a href="#annotated-cell-17-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="fu">all_of</span>(all_cols)) <span class="sc">|></span> </span>
<span id="annotated-cell-17-18"><a href="#annotated-cell-17-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(year <span class="sc">%in%</span> <span class="fu">c</span>(<span class="dv">2005</span><span class="sc">:</span><span class="dv">2020</span>)) <span class="sc">|></span> </span>
<span id="annotated-cell-17-19"><a href="#annotated-cell-17-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">site =</span> <span class="fu">rep</span>(site_name_formatted)) <span class="sc">|></span> </span>
<span id="annotated-cell-17-20"><a href="#annotated-cell-17-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date, year, month, day, <span class="at">sep =</span> <span class="st">"-"</span>, <span class="at">remove =</span> <span class="cn">FALSE</span>) <span class="sc">|></span> </span>
<span id="annotated-cell-17-21"><a href="#annotated-cell-17-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">time =</span> <span class="fu">times</span>(decimal_time)) <span class="sc">|></span> </span>
<span id="annotated-cell-17-22"><a href="#annotated-cell-17-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> date_time, date, time, <span class="at">sep =</span> <span class="st">" "</span>, <span class="at">remove =</span> <span class="cn">TRUE</span>) <span class="sc">|></span> </span>
<span id="annotated-cell-17-23"><a href="#annotated-cell-17-23" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">date_time =</span> <span class="fu">as.POSIXct</span>(date_time, <span class="st">"%Y-%m-%d %H:%M:%S"</span>, <span class="at">tz =</span> <span class="st">"GMT"</span>),</span>
<span id="annotated-cell-17-24"><a href="#annotated-cell-17-24" aria-hidden="true" tabindex="-1"></a> <span class="at">year =</span> <span class="fu">as.factor</span>(year),</span>
<span id="annotated-cell-17-25"><a href="#annotated-cell-17-25" aria-hidden="true" tabindex="-1"></a> <span class="at">month =</span> <span class="fu">as.factor</span>(month),</span>
<span id="annotated-cell-17-26"><a href="#annotated-cell-17-26" aria-hidden="true" tabindex="-1"></a> <span class="at">day =</span> <span class="fu">as.numeric</span>(day)) <span class="sc">|></span></span>
<span id="annotated-cell-17-27"><a href="#annotated-cell-17-27" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">month_name =</span> <span class="fu">as.factor</span>(month.name[month])) <span class="sc">|></span></span>
<span id="annotated-cell-17-28"><a href="#annotated-cell-17-28" aria-hidden="true" tabindex="-1"></a> <span class="fu">replace_with_na</span>(<span class="at">replace =</span> <span class="fu">list</span>(<span class="at">Temp_bot =</span> <span class="dv">9999</span>, <span class="at">Temp_top =</span> <span class="dv">9999</span>, <span class="at">Temp_mid =</span> <span class="dv">9999</span>)) <span class="sc">|></span> </span>
<span id="annotated-cell-17-29"><a href="#annotated-cell-17-29" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="fu">any_of</span>(<span class="fu">c</span>(<span class="st">"site"</span>, <span class="st">"date_time"</span>, <span class="st">"year"</span>, <span class="st">"month"</span>, <span class="st">"day"</span>, <span class="st">"month_name"</span>, <span class="st">"Temp_bot"</span>, <span class="st">"Temp_mid"</span>, <span class="st">"Temp_top"</span>)))</span>
<span id="annotated-cell-17-30"><a href="#annotated-cell-17-30" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-17-31"><a href="#annotated-cell-17-31" aria-hidden="true" tabindex="-1"></a> <span class="co"># return cleaned df ----</span></span>
<span id="annotated-cell-17-32"><a href="#annotated-cell-17-32" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(temps_clean)</span>
<span id="annotated-cell-17-33"><a href="#annotated-cell-17-33" aria-hidden="true" tabindex="-1"></a> </span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-17" data-target-annotation="2" onclick="event.preventDefault();" href="">2</a><span id="annotated-cell-17-34" class="code-annotation-target"><a href="#annotated-cell-17-34" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="annotated-cell-17-35"><a href="#annotated-cell-17-35" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-17-36"><a href="#annotated-cell-17-36" aria-hidden="true" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"The data frame provided does not include the necessary columns. Double check your data!"</span>)</span>
<span id="annotated-cell-17-37"><a href="#annotated-cell-17-37" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-17-38"><a href="#annotated-cell-17-38" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="annotated-cell-17-39"><a href="#annotated-cell-17-39" aria-hidden="true" tabindex="-1"></a> </span>
<span id="annotated-cell-17-40"><a href="#annotated-cell-17-40" aria-hidden="true" tabindex="-1"></a>}</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-17" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="4,6,32" data-code-annotation="1" data-code-cell="annotated-cell-17">add an <code>if else</code> statement that checks whether the necessary columns are present in the raw data – if yes, proceed with data cleaning</span>
</dd>
<dt data-target-cell="annotated-cell-17" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="34,36,38" data-code-annotation="2" data-code-cell="annotated-cell-17">if no, throw an error message</span>
</dd>
</dl>
</div>
</div>
<p>Let’s rerun our function and try it out one last time:</p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-18"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-18-1"><a href="#annotated-cell-18-1" aria-hidden="true" tabindex="-1"></a><span class="co"># functions_pipeline.R #</span></span>
<span id="annotated-cell-18-2"><a href="#annotated-cell-18-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-18-3"><a href="#annotated-cell-18-3" aria-hidden="true" tabindex="-1"></a><span class="co">#..........................load packages......................... </span></span>
<span id="annotated-cell-18-4"><a href="#annotated-cell-18-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(readr)</span>
<span id="annotated-cell-18-5"><a href="#annotated-cell-18-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr) </span>
<span id="annotated-cell-18-6"><a href="#annotated-cell-18-6" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyr)</span>
<span id="annotated-cell-18-7"><a href="#annotated-cell-18-7" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(stringr) </span>
<span id="annotated-cell-18-8"><a href="#annotated-cell-18-8" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(chron)</span>
<span id="annotated-cell-18-9"><a href="#annotated-cell-18-9" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(naniar)</span>
<span id="annotated-cell-18-10"><a href="#annotated-cell-18-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-18-11"><a href="#annotated-cell-18-11" aria-hidden="true" tabindex="-1"></a><span class="co">#........................source functions........................ </span></span>
<span id="annotated-cell-18-12"><a href="#annotated-cell-18-12" aria-hidden="true" tabindex="-1"></a><span class="fu">source</span>(<span class="st">"utils/clean_ocean_temps.R"</span>) </span>
<span id="annotated-cell-18-13"><a href="#annotated-cell-18-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-18-14"><a href="#annotated-cell-18-14" aria-hidden="true" tabindex="-1"></a><span class="co">#..........................import data........................... </span></span>
<span id="annotated-cell-18-15"><a href="#annotated-cell-18-15" aria-hidden="true" tabindex="-1"></a>alegria <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"alegria_mooring_ale_20210617.csv"</span>))</span>
<span id="annotated-cell-18-16"><a href="#annotated-cell-18-16" aria-hidden="true" tabindex="-1"></a>mohawk <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"mohawk_mooring_mko_20220330.csv"</span>))</span>
<span id="annotated-cell-18-17"><a href="#annotated-cell-18-17" aria-hidden="true" tabindex="-1"></a>carpinteria <span class="ot"><-</span> <span class="fu">read_csv</span>(here<span class="sc">::</span><span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"raw_data"</span>, <span class="st">"carpinteria_mooring_car_20220330.csv"</span>))</span>
<span id="annotated-cell-18-18"><a href="#annotated-cell-18-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-18-19"><a href="#annotated-cell-18-19" aria-hidden="true" tabindex="-1"></a><span class="co">#...........................clean data........................... </span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-18" data-target-annotation="1" onclick="event.preventDefault();" href="">1</a><span id="annotated-cell-18-20" class="code-annotation-target"><a href="#annotated-cell-18-20" aria-hidden="true" tabindex="-1"></a>alegria_clean <span class="ot"><-</span> <span class="fu">clean_ocean_temps</span>(<span class="at">raw_data =</span> alegria, <span class="at">site_name =</span> <span class="st">"alegria"</span>, <span class="at">include_temps =</span> <span class="fu">c</span>(<span class="st">"Temp_bot"</span>))</span>
<span id="annotated-cell-18-21"><a href="#annotated-cell-18-21" aria-hidden="true" tabindex="-1"></a>mohawk_clean <span class="ot"><-</span> <span class="fu">clean_ocean_temps</span>(<span class="at">raw_data =</span> mohawk, <span class="at">site_name =</span> <span class="st">"MOHAWK"</span>)</span>
<span id="annotated-cell-18-22"><a href="#annotated-cell-18-22" aria-hidden="true" tabindex="-1"></a>carpinteria_clean <span class="ot"><-</span> <span class="fu">clean_ocean_temps</span>(<span class="at">raw_data =</span> carpinteria, <span class="at">site_name =</span> <span class="st">"Carpinteria"</span>, <span class="at">include_temps =</span> <span class="fu">c</span>(<span class="st">"Temp_mid"</span>, <span class="st">"Temp_bot"</span>))</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-18" data-target-annotation="2" onclick="event.preventDefault();" href="">2</a><span id="annotated-cell-18-23" class="code-annotation-target"><a href="#annotated-cell-18-23" aria-hidden="true" tabindex="-1"></a>penguins_clean <span class="ot"><-</span> <span class="fu">clean_ocean_temps</span>(<span class="at">raw_data =</span> palmerpenguins<span class="sc">::</span>penguins)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-18" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="20,21,22" data-code-annotation="1" data-code-cell="annotated-cell-18">these three should work as intended</span>
</dd>
<dt data-target-cell="annotated-cell-18" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="23" data-code-annotation="2" data-code-cell="annotated-cell-18">this one should throw an error!</span>
</dd>
</dl>
</div>
</div>
</section>
</section>
<section id="ii-write-a-function-to-plot-data-sets" class="level2">
<h2 class="anchored" data-anchor-id="ii-write-a-function-to-plot-data-sets"><strong>ii:</strong> Write a function to <strong>plot</strong> data sets</h2>
<p>Similar to what we did for our cleaning code, let’s first identify which parts of the plotting code need to be generalized/made “flexible” so that any of our three cleaned data frames can be passed to it for plotting. For us, that’s the <strong>name of the clean data frame</strong> and the <strong>site name</strong> that appears in the plot title (see annotation notes below the rendered code):</p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-19"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-19" data-target-annotation="1" onclick="event.preventDefault();" href="">1</a><span id="annotated-cell-19-1" class="code-annotation-target"><a href="#annotated-cell-19-1" aria-hidden="true" tabindex="-1"></a>alegria_plot <span class="ot"><-</span> alegria_clean <span class="sc">|></span></span>
<span id="annotated-cell-19-2"><a href="#annotated-cell-19-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(month_name) <span class="sc">|></span> </span>
<span id="annotated-cell-19-3"><a href="#annotated-cell-19-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> Temp_bot, <span class="at">y =</span> month_name, <span class="at">fill =</span> <span class="fu">after_stat</span>(x))) <span class="sc">+</span></span>
<span id="annotated-cell-19-4"><a href="#annotated-cell-19-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_density_ridges_gradient</span>(<span class="at">rel_min_height =</span> <span class="fl">0.01</span>, <span class="at">scale =</span> <span class="dv">3</span>) <span class="sc">+</span> </span>
<span id="annotated-cell-19-5"><a href="#annotated-cell-19-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">9</span>, <span class="dv">12</span>, <span class="dv">15</span>, <span class="dv">18</span>, <span class="dv">21</span>)) <span class="sc">+</span></span>
<span id="annotated-cell-19-6"><a href="#annotated-cell-19-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_discrete</span>(<span class="at">limits =</span> <span class="fu">rev</span>(month.name)) <span class="sc">+</span> </span>
<span id="annotated-cell-19-7"><a href="#annotated-cell-19-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_gradientn</span>(<span class="at">colors =</span> <span class="fu">c</span>(<span class="st">"#2C5374"</span>,<span class="st">"#778798"</span>, <span class="st">"#ADD8E6"</span>, <span class="st">"#EF8080"</span>, <span class="st">"#8B3A3A"</span>), <span class="at">name =</span> <span class="st">"Temp. (°C)"</span>) <span class="sc">+</span></span>
<span id="annotated-cell-19-8"><a href="#annotated-cell-19-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Bottom Temperature (°C)"</span>,</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-19" data-target-annotation="2" onclick="event.preventDefault();" href="">2</a><span id="annotated-cell-19-9" class="code-annotation-target"><a href="#annotated-cell-19-9" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Bottom Temperatures at Alegria Reef, Santa Barbara, CA"</span>,</span>
<span id="annotated-cell-19-10"><a href="#annotated-cell-19-10" aria-hidden="true" tabindex="-1"></a> <span class="at">subtitle =</span> <span class="st">"Temperatures (°C) aggregated by month from 2005 - 2020"</span>) <span class="sc">+</span></span>
<span id="annotated-cell-19-11"><a href="#annotated-cell-19-11" aria-hidden="true" tabindex="-1"></a> ggridges<span class="sc">::</span><span class="fu">theme_ridges</span>(<span class="at">font_size =</span> <span class="dv">13</span>, <span class="at">grid =</span> <span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="annotated-cell-19-12"><a href="#annotated-cell-19-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="annotated-cell-19-13"><a href="#annotated-cell-19-13" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.title.y =</span> <span class="fu">element_blank</span>()</span>
<span id="annotated-cell-19-14"><a href="#annotated-cell-19-14" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="annotated-cell-19-15"><a href="#annotated-cell-19-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-19-16"><a href="#annotated-cell-19-16" aria-hidden="true" tabindex="-1"></a>alegria_plot</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-19" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="1" data-code-annotation="1" data-code-cell="annotated-cell-19">the name of the clean data frame needs to be generalized</span>
</dd>
<dt data-target-cell="annotated-cell-19" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="9" data-code-annotation="2" data-code-cell="annotated-cell-19">the site name that appears in the plot title needs to be generalized</span>
</dd>
</dl>
</div>
</div>
<section id="version-1-1" class="level3">
<h3 class="anchored" data-anchor-id="version-1-1"><strong>Version 1:</strong></h3>
<p><strong><em>The primary goal of <code>plot_ocean_temps</code> v1 is to get a basic plotting function working.</em></strong></p>
<p>Now we can begin to building our function. Let’s again start by creating the skeleton of our function, which we’ll call <code>plot_ocean_temps()</code>:</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>plot_ocean_temps <span class="ot"><-</span> <span class="cf">function</span>(){</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb10-3"><a href="#cb10-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>
<p>…then copy and paste our plotting code for the clean Alegria Reef data into the body of the function (i.e. within the curly brackets, <code>{}</code>).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>plot_ocean_temps <span class="ot"><-</span> <span class="cf">function</span>() {</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a> alegria_plot <span class="ot"><-</span> alegria_clean <span class="sc">|></span> </span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(month_name) <span class="sc">|></span> </span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> Temp_bot, <span class="at">y =</span> month_name, <span class="at">fill =</span> <span class="fu">after_stat</span>(x))) <span class="sc">+</span></span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_density_ridges_gradient</span>(<span class="at">rel_min_height =</span> <span class="fl">0.01</span>, <span class="at">scale =</span> <span class="dv">3</span>) <span class="sc">+</span> </span>
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">9</span>, <span class="dv">12</span>, <span class="dv">15</span>, <span class="dv">18</span>, <span class="dv">21</span>)) <span class="sc">+</span></span>
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_discrete</span>(<span class="at">limits =</span> <span class="fu">rev</span>(month.name)) <span class="sc">+</span> </span>
<span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_gradientn</span>(<span class="at">colors =</span> <span class="fu">c</span>(<span class="st">"#2C5374"</span>,<span class="st">"#778798"</span>, <span class="st">"#ADD8E6"</span>, <span class="st">"#EF8080"</span>, <span class="st">"#8B3A3A"</span>), <span class="at">name =</span> <span class="st">"Temp. (°C)"</span>) <span class="sc">+</span></span>
<span id="cb11-10"><a href="#cb11-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Bottom Temperature (°C)"</span>,</span>
<span id="cb11-11"><a href="#cb11-11" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Bottom Temperatures at Alegria Reef, Santa Barbara, CA"</span>,</span>
<span id="cb11-12"><a href="#cb11-12" aria-hidden="true" tabindex="-1"></a> <span class="at">subtitle =</span> <span class="st">"Temperatures (°C) aggregated by month from 2005 - 2020"</span>) <span class="sc">+</span></span>
<span id="cb11-13"><a href="#cb11-13" aria-hidden="true" tabindex="-1"></a> ggridges<span class="sc">::</span><span class="fu">theme_ridges</span>(<span class="at">font_size =</span> <span class="dv">13</span>, <span class="at">grid =</span> <span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb11-14"><a href="#cb11-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb11-15"><a href="#cb11-15" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.title.y =</span> <span class="fu">element_blank</span>()</span>
<span id="cb11-16"><a href="#cb11-16" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb11-17"><a href="#cb11-17" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb11-18"><a href="#cb11-18" 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>Just like in <code>clean_ocean_temps()</code>, we want the ability to provide our function with any three of our cleaned data sets for plotting. To do this, we’ll make the following modifications:</p>