-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndex.html
3922 lines (3737 loc) · 323 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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="progressive" content="false" />
<meta name="allow-skip" content="true" />
<meta name="learnr-version-prerender" content="0.11.5" />
<title>Introduction to R, from wrangling to visualization</title>
<!-- header-includes START -->
<!-- HEAD_CONTENT -->
<!-- header-includes END -->
<!-- HEAD_CONTENT -->
<!-- highlightjs -->
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<!-- taken from https://github.com/rstudio/rmarkdown/blob/de8a9c38618903627ca509f5401d50a0876079f7/inst/rmd/h/default.html#L293-L343 -->
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- end tabsets -->
</head>
<body>
<a class='sr-only sr-only-focusable visually-hidden-focusable' href='#learnr-tutorial-content'>Skip to Tutorial Content</a>
<div class="pageContent band">
<main class="bandContent page">
<article class="topics" id="learnr-tutorial-content">
<!-- Warning: there must always be a new line before a code chunk and end of file -->
<!-- If not the whole learnr puts odd columns across your html file -->
<!-- Also, we can have either `solutions` or `hints`, not both -->
<p><img src="images/INRES_Logo.png" style="width:2in" /> <img
src="images/Uni_Bonn_Picture.png" style="width:2in" /> <img
src="images/ZEF_Logo.png" style="width:2in" /></p>
<!-- List of Lecture RMD files to be compiled -->
<div id="section-introduction" class="section level2">
<h2>Introduction</h2>
<p>Welcome to this interactive tutorial on the R programming language.
This tutorial runs in the <code>learnr</code> <span
class="citation">(Aden-Buie et al. 2023)</span> library and provides a
<code>shiny</code> interface <span class="citation">(Chang et al.
2023)</span> that will allow you the chance to interact with the
examples in the R programming language. You may follow along by simply
working in this browser. You may find the content and learning a bit
more useful if you work on your own machine as well. This will require
loading the necessary software. For this tutorial we will use functions
from <code>tidyverse</code> <span class="citation">(Wickham 2023)</span>
libraries including <code>ggplot2</code> <span class="citation">(Wickham
et al. 2024)</span>, <code>dplyr</code> <span class="citation">(Wickham
et al. 2023)</span> and <code>magrittr</code> <span
class="citation">(Bache and Wickham 2022)</span> among others in the <a
href="https://www.r-project.org/">R programming language</a> <span
class="citation">(R Core Team 2023)</span>.</p>
<p>If you are also planning to run this on your own machine then you
should start by installing and running <a
href="https://www.r-project.org/">R</a> and <a
href="https://rstudio.com/">RStudio</a>. This video walks you through
that process.</p>
<!-- ![](https://youtu.be/WT3tKlzCZgo) -->
<iframe width="560" height="315" src="https://www.youtube.com/embed/WT3tKlzCZgo" frameborder="0" allowfullscreen>
</iframe>
<div id="section-using-this-tutorial" class="section level3">
<h3>Using this tutorial</h3>
<p>Here is an example of how this tutorial works. The code below
calculates the answer to one plus one.</p>
<div class="tutorial-exercise" data-label="example-addition"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Change this code so it computes two plus two
1 + 1</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="example-addition-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>2 + 2</code></pre>
</div>
<p>For each example there will also generally be a small exercise after
the <code>#</code> mark. We will ask you to work in this interface and
make changes to the existing code. In this interface you will see
options for looking at the solution and sometimes some hints. Feel free
to run these here in the web interface and also to run them on your own
machine.</p>
<p>You will also always have the option to get a copy of the code to run
on your own machine. To do that you will need all the libraries. If
these do not load you will also need to use the
<code>install.packages()</code> function.
i.e. <code>install.packages("datasauRus")</code>.</p>
<div class="tutorial-exercise" data-label="example-libraries"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code>library(datasauRus)
library(gganimate)
library(ggplot2)
library(purrr)
library(rmarkdown)
library(tidyverse)
# part of the tidyverse:
library(dplyr)
library(magrittr)</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
</div>
<div id="section-bonus-rstudio-settings" class="section level3">
<h3>Bonus RStudio settings</h3>
<p>You will see from the examples throughout the course that I use dark
mode in RStudio. You can find these settings and choose the one you like
best under <em>Global Options > Appearance</em>. For my display I use
the ‘Pastel On Dark’ option.</p>
<p><img src="images/dark_mode.png" style="width:3in" /></p>
<p>Using color in code is nice and can also help you troubleshoot code
that uses lots of nested parentheses <code>()</code>, square brackets
<code>[]</code> or curly braces <code>{}</code>. You can adjust these
settings under <em>Global Options > Code > Display</em>.</p>
<p><img src="images/rainbow_parentheses.png" style="width:3in" /></p>
</div>
</div>
<div id="section-data-wrangling-part-1" class="section level2">
<h2>Data Wrangling Part 1</h2>
<p><img src="images/wrangler.png" style="width:2in" /> <img
src="images/tidyR.png" style="width:1in" /> <img src="images/dplyr.png"
style="width:1.5in" /></p>
<p>The video below gives a brief overview of the content covered in this
section of the tutorial. All the <a
href="http://htmlpreview.github.io/?https://github.com/CWWhitney/teaching_R/blob/master/Data_Wrangling_Rpres/Data_Wrangling.html#/">Data
Wrangling slides</a> are also available as a web page.</p>
<!-- ![](https://youtu.be/j6XRof3q9aM) -->
<iframe width="560" height="315" src="https://www.youtube.com/embed/j6XRof3q9aM" frameborder="0" allowfullscreen>
</iframe>
<div id="section-notes-on-r" class="section level3">
<h3>Notes on R</h3>
<ul>
<li>Tidy code style using tidyR</li>
<li>Clean and intuitive functions using dplyr</li>
<li>Concise code using magrittr ‘Ceci n’est pas une pipe’</li>
</ul>
<p><img src="images/magrittr.png" style="width:1in" /></p>
</div>
<div id="section-notes-on-r-about-process" class="section level3">
<h3>Notes on R: About process</h3>
<ul>
<li><p>“[…] writing R code is a hedonistically artistic, left-brained,
paint-in-your-hair sort of experience […]</p></li>
<li><p>learn how to code the same way we learned how to catch
salamanders as children – trial and error, flipping over rocks till we
get a reward […]</p></li>
<li><p>once the ecstasy of creation has swept over us, we awake late the
next morning to find our canvas covered with 2100 lines of R code
[…]</p></li>
<li><p>Heads throbbing with a statistical absinthe hangover, we trudge
through it slowly over days, trying to figure out what we did.”</p>
<p><em>Andrew MacDonald</em></p></li>
</ul>
</div>
<div id="section-notes-on-r-focus" class="section level3">
<h3>Notes on R: Focus</h3>
<p><img src="images/Focus.png" style="width:5in" /></p>
</div>
<div id="section-notes-on-r-keeping-track-of-work"
class="section level3">
<h3>Notes on R: Keeping track of work</h3>
<p><img src="images/tidyR.png" style="width:1in" /></p>
<p>Keep it tidy</p>
<p>When writing .R files use the <code>#</code> symbol to annotate and
not run lines. This is a great way to make notes for others and for
future you.</p>
<p>We will talk later about using other file types like
<code>Rmarkdown</code> for organizing R script and other associated
languages. There it will be possible to add a lot more information and
text, citations etc. An .R file is intended for code but we can still
keep it organized in sections by ending headers with <code>----</code>
or <code>####</code> annotation.</p>
<p><code># Section 1 ----</code></p>
<p><code># Section 2 ####</code></p>
<p><code># Section 3 ####</code></p>
<p>Look for the <code>Table of contents</code> in the upper right
console of the RStudio scripting pane (next to the <code>Run</code>
button).</p>
<p><img src="images/toc_rstudio.png" style="width:2in" /></p>
<p>Read more tips in the <a href="http://style.tidyverse.org/">Tidyverse
Style guide</a>.</p>
</div>
</div>
<div id="section-data-wrangling-part-2" class="section level2">
<h2>Data Wrangling Part 2</h2>
<p><img src="images/wrangler.png" style="width:1in" /> <img
src="images/tidyR.png" style="width:1in" /> <img src="images/dplyr.png"
style="width:1.5in" /></p>
<p>The video below offers a brief overview of the content covered in
this section of the tutorial. Feel free to watch the video and follow
along or simply work through the tutorial.</p>
<!-- ![](https://youtu.be/8vqQljFcMsw) -->
<iframe width="560" height="315" src="https://www.youtube.com/embed/8vqQljFcMsw" frameborder="0" allowfullscreen>
</iframe>
<div id="section-notes-on-tidy-r" class="section level3">
<h3>Notes on tidy R</h3>
<p><img src="images/tidyR.png" style="width:1in" /></p>
<p>Keep it tidy</p>
<p>If you are following this tutorial by running the code on your local
machine (recommended) then it may make sense to check your R version by
running the following code in your R console:</p>
<div class="tutorial-exercise" data-label="example-version"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code>version</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<p>At the time of writing this I am using R version 4.0.4
<code>Lost Library Book</code> <span class="citation">(R Core Team
2023)</span>. If you do not have this version or something newer it may
make sense to update so that you can follow along without pesky version
issues.</p>
<p>The easiest way to get libraries for today is to install the whole
tidyverse <span class="citation">(Wickham 2023)</span> by typing
<code>install.packages("tidyverse")</code> in the R console and then
running <code>library(tidyverse)</code>:</p>
<pre><code>install.packages("tidyverse")
library(tidyverse)</code></pre>
<p>If you save your work to an .R file (recommended) be sure to annotate
any code that you do not intend to run each time with the <code>#</code>
symbol. You should only need to install tidyverse once and should be
sure to either change that line of code to
<code>#install.packages("tidyverse")</code> or remove it from your
script.</p>
<p>Read more style tips in the <a
href="http://style.tidyverse.org/">tidyverse style guide</a> <span
class="citation">(Wickham 2023)</span>.</p>
</div>
<div id="section-notes-on-tidy-r-browsevignettes"
class="section level3">
<h3>Notes on tidy R browseVignettes</h3>
<p><img src="images/tidyR.png" style="width:1in" /></p>
<p>Keep it tidy</p>
<p>Get a lot of examples and details about the tidyverse by running the
following code in the R console:
<code>browseVignettes(package = "tidyverse")</code>. Nearly every R
library has a collection of vignettes that walk through examples and
show, often in explicit detail, the authors intended use of the
library.</p>
</div>
<div id="section-the-tidy-tools-manifesto" class="section level3">
<h3>The tidy tools manifesto</h3>
<p>In this tutorial we will be following the basic ideas behind the
tidyverse.</p>
<p><img src="images/tidy_paper.jpg" style="width:5in" /></p>
<p>Read the <a
href="https://cran.r-project.org/web/packages/tidyverse/vignettes/manifesto.html">full
tidyverse manifesto here</a>.</p>
</div>
<div id="section-notes-on-r-tidyr-process" class="section level3">
<h3>Notes on R: tidyR process</h3>
<p><img src="images/tidyR.png" style="width:1in" /></p>
<p>Keep it tidy</p>
<p><img src="images/tidyR_process.png" style="width:4in" /></p>
<ul>
<li>Good coding style is like correct punctuation:</li>
<li>withoutitthingsarehardtoread</li>
<li>When your data is tidy, each column is a variable, and each row is
an observation</li>
<li>Consistent structure lets you focus your struggle on questions about
the data, not fighting to get the data into the right form for different
functions</li>
</ul>
<p><img src="images/wrangler.png" style="width:2in" /></p>
<p>Read more style tipes in the <a
href="http://style.tidyverse.org/">tidyverse style guide</a> <span
class="citation">(Wickham 2023)</span>.</p>
</div>
<div id="section-notes-on-r-tidy-data" class="section level3">
<h3>Notes on R: Tidy Data</h3>
<p>Three things make a dataset tidy:</p>
<ul>
<li>Each variable with its own column.</li>
<li>Each observation with its own row.</li>
<li>Each value with its own cell.</li>
</ul>
<p><img src="images/tidydata.png" style="width:5in" /></p>
<p>Read more about this from <a
href="www.jstatsoft.org/v59/i10/paper">Wickham’s paper</a> in the
Journal of Statistical Software.</p>
</div>
<div id="section-wrangling-transform" class="section level3">
<h3>Wrangling: transform</h3>
<ul>
<li>Once you have <strong>tidy</strong> data, a common first step is to
<strong>transform</strong> it</li>
<li>narrowing in on observations of interest</li>
<li>creating new variables that are functions of existing variables</li>
<li>calculating a set of summary statistics</li>
</ul>
<p><img src="images/Wrangling_Data.png" style="width:3in" /></p>
<p>www.codeastar.com/data-wrangling/</p>
</div>
<div id="section-wrangling-dplyr-arguments" class="section level3">
<h3>Wrangling: dplyr arguments</h3>
<p>Format of <strong>dplyr</strong></p>
<p><img src="images/dplyr.png" style="width:1in" /></p>
<p>Arguments start with a data frame</p>
<ul>
<li><strong>select</strong>: return a subset of the columns</li>
<li><strong>filter</strong>: extract a subset of rows</li>
<li><strong>rename</strong>: rename variables</li>
<li><strong>mutate</strong>: add new variables and columns or
transform</li>
<li><strong>group_by</strong>: split data into groups</li>
<li><strong>summarize</strong>: generate tables of summary
statistics</li>
</ul>
<p><a href="https://dplyr.tidyverse.org/"
class="uri">https://dplyr.tidyverse.org/</a></p>
</div>
<div id="section-getting-your-data-in-r" class="section level3">
<h3>Getting your data in R</h3>
<p>Load data</p>
<p><img src="images/R_logo.png" style="width:1in" /></p>
<p>The data we will use for this course is <a
href="https://raw.githubusercontent.com/CWWhitney/teaching_R/master/participants_data.csv">on
Github</a> and you can save it as a .csv to your local folder.</p>
<ul>
<li>Load the data using the <code>read.csv</code> function</li>
</ul>
<pre><code># Use this on your machine
participants_data <- read.csv("participants_data.csv")</code></pre>
<p>Learn more about what this function does by typing
<code>?read.csv</code> in the R console.</p>
<p>You can also get the data from this Github repository by using the
<code>read_csv</code> function from the <code>readr</code> library <span
class="citation">(Wickham, Hester, and Bryan 2023)</span> and
<code>url</code> function from base R. In this case you will want to use
the ‘save as’ option for the webpage so that you can have it stored
locally as a <code>comma separated values</code> (.csv) file on your
machine.</p>
<div class="tutorial-exercise" data-label="wrangle2-git-data"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code>library(readr)
urlfile = "https://raw.githubusercontent.com/CWWhitney/teaching_R/master/participants_data.csv"
participants_data <- read_csv(url(urlfile))</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<ul>
<li>Keep your data in the same folder structure as .RProj</li>
<li>at or below the level of .RProj</li>
</ul>
</div>
<div id="section-looking-at-the-data" class="section level3">
<h3>Looking at the data</h3>
<ul>
<li>View the full data in the console (see the <code>View</code>
function to see it in the Rstudio ‘Environment’)</li>
</ul>
<div class="tutorial-exercise"
data-label="wrangle2-names_participants-data" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>participants_data</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<ul>
<li>Look at the top rows of the data with the <code>head</code>
function. The default of the <code>head</code> function is to show 6
rows. This can be changed with the <code>n</code> argument.</li>
</ul>
<div class="tutorial-exercise"
data-label="wrangle2-head-participants-data" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code># Change the number of rows displayed to 7
head(participants_data,
n = 4)</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-head-participants-data-hint-1" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code># use the ?head option to learn the details of the function
?head</code></pre>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-head-participants-data-hint-2" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code># look at the 'Arguments' section for the 'n' argument
?head</code></pre>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-head-participants-data-hint-3" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code># The 'n' argument should be changed from 'n = 4' to 'n = 7'
head(participants_data,
n = 7)</code></pre>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-head-participants-data-solution"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code>head(participants_data,
n = 7)</code></pre>
</div>
<ul>
<li>Check the names of the variables in the data with the
<code>names</code> function</li>
</ul>
<div class="tutorial-exercise"
data-label="wrangle2-names-participants-data" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>names(participants_data)</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<ul>
<li>Look at the structure of the data with the <code>str</code>
function</li>
</ul>
<div class="tutorial-exercise"
data-label="wrangle2-str-participants-data" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>str(participants_data)</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<ul>
<li>Call a particular variable in your data with <code>$</code></li>
</ul>
<div class="tutorial-exercise" data-label="wrangle2-call-variable"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Change the variable to gender
participants_data$age</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-call-variable-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>participants_data$gender</code></pre>
</div>
<p>Follow these steps to see the result of the rest of the
transformations we perform with <code>tidyverse</code>.</p>
</div>
<div id="section-wrangling-dplyr-library" class="section level3">
<h3>Wrangling: dplyr library</h3>
<p>Using <strong>dplyr</strong> <img src="images/dplyr.png"
style="width:1in" /></p>
<p>Load the dplyr library by running <code>library(dplyr)</code> in the
R console. do the same for other libraries we need today
<code>library(tidyr)</code> and <code>library(magrittr)</code> <span
class="citation">Wickham et al. (2023)</span>.</p>
<p>Inspiration for many of the following materials comes from Roger
Peng’s <a href="genomicsclass.github.io/book/pages/dplyr_tutorial">dplyr
tutorial</a>.</p>
<p><img src="images/github.png" style="width:1in" /></p>
<p>Read more about the <a href="https://dplyr.tidyverse.org/">dplyr
library</a> <span class="citation">(Wickham et al. 2023)</span>.</p>
</div>
<div id="section-wrangling-dplyrselect-aca_work_set"
class="section level3">
<h3>Wrangling: dplyr::select aca_work_set</h3>
<p>Subsetting</p>
<p><img src="images/dplyr.png" style="width:1in" /></p>
<p><strong>Select</strong></p>
<p>Create a subset of the data with the <code>select</code>
function:</p>
<div class="tutorial-exercise" data-label="wrangle2-select-aca-parents"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Change the selection to batch and age
select(participants_data,
academic_parents,
working_hours_per_day)</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-select-aca-parents-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>select(participants_data,
batch,
age)</code></pre>
</div>
<p><a href="https://dplyr.tidyverse.org/"
class="uri">https://dplyr.tidyverse.org/</a></p>
</div>
<div id="section-wrangling-dplyrselect-non_aca_work_filter"
class="section level3">
<h3>Wrangling: dplyr::select non_aca_work_filter</h3>
<p>Subsetting <img src="images/dplyr.png" style="width:1in" /></p>
<p><strong>Select</strong></p>
<p>Try creating a subset of the data with the <code>select</code>
function:</p>
<div class="tutorial-exercise"
data-label="wrangle2-select-non-aca-parents" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code># Change the selection
# without batch and age
select(participants_data,
-academic_parents,
-working_hours_per_day)</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-select-non-aca-parents-solution"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code>select(participants_data,
-batch,
-age)</code></pre>
</div>
<p><a href="https://dplyr.tidyverse.org/"
class="uri">https://dplyr.tidyverse.org/</a></p>
</div>
<div id="section-wrangling-dplyrfilter-work_filter"
class="section level3">
<h3>Wrangling: dplyr::filter work_filter</h3>
<p>Subsetting <img src="images/dplyr.png" style="width:1in" /></p>
<p><strong>Filter</strong></p>
<p>Try creating a subset of the data with the <code>filter</code>
function:</p>
<div class="tutorial-exercise" data-label="wrangle2-filter-work"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Change the selection to
# those who work more than 5 hours a day
filter(participants_data,
working_hours_per_day >10)</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-filter-work-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>filter(participants_data,
working_hours_per_day >5)</code></pre>
</div>
<p><a href="https://dplyr.tidyverse.org/"
class="uri">https://dplyr.tidyverse.org/</a></p>
</div>
<div id="section-wrangling-dplyrfilter-work_name_filter"
class="section level3">
<h3>Wrangling: dplyr::filter work_name_filter</h3>
<p>Subsetting <img src="images/dplyr.png" style="width:1in" /></p>
<p><strong>Filter</strong></p>
<p>Create a subset of the data with multiple options in the
<code>filter</code> function:</p>
<div class="tutorial-exercise" data-label="wrangle2-filter-work-name"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Change the filter to those who
# work more than 5 hours a day and
# names are longer than three letters
filter(participants_data,
working_hours_per_day >10 &
letters_in_first_name >6)</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-filter-work-name-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>filter(participants_data,
working_hours_per_day >5 &
letters_in_first_name >3)</code></pre>
</div>
<p><a href="https://dplyr.tidyverse.org/"
class="uri">https://dplyr.tidyverse.org/</a></p>
</div>
<div id="section-wrangling-dplyrrename-name_length"
class="section level3">
<h3>Wrangling: dplyr::rename name_length</h3>
<p><strong>Rename</strong> <img src="images/dplyr.png"
style="width:1in" /></p>
<p>Change the names of the variables in the data with the
<code>rename</code> function:</p>
<div class="tutorial-exercise" data-label="wrangle2-rename-letters"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Rename the variable km_home_to_office as commute
rename(participants_data,
name_length = letters_in_first_name)</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-rename-letters-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>rename(participants_data,
commute = km_home_to_office)</code></pre>
</div>
<p><a href="https://dplyr.tidyverse.org/"
class="uri">https://dplyr.tidyverse.org/</a></p>
</div>
<div id="section-wrangling-dplyrmutate" class="section level3">
<h3>Wrangling: dplyr::mutate</h3>
<p><strong>Mutate</strong> <img src="images/dplyr.png"
style="width:1in" /></p>
<div class="tutorial-exercise" data-label="wrangle2-rename-work"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Mutate a new column named age_mean that is a function of the age multiplied by the mean of all ages in the group
mutate(participants_data,
labor_mean = working_hours_per_day*
mean(working_hours_per_day))</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-rename-work-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>mutate(participants_data,
age_mean = age*
mean(age))</code></pre>
</div>
<p><a href="https://dplyr.tidyverse.org/"
class="uri">https://dplyr.tidyverse.org/</a></p>
</div>
<div id="section-wrangling-dplyrmutate-1" class="section level3">
<h3>Wrangling: dplyr::mutate</h3>
<p><strong>Mutate</strong> <img src="images/dplyr.png"
style="width:1in" /></p>
<p>Create a commute category with the <code>mutate</code> function:</p>
<div class="tutorial-exercise" data-label="wrangle2-mutate-commute"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Mutate new column named response_speed
# populated by 'slow' if it took you
# more than a day to answer my email and
# 'fast' for others
mutate(participants_data,
commute =
ifelse(km_home_to_office > 10,
"commuter", "local"))</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-mutate-commute-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>mutate(participants_data,
response_speed =
ifelse(days_to_email_response > 1,
"slow", "fast"))</code></pre>
</div>
<p><a href="https://dplyr.tidyverse.org/"
class="uri">https://dplyr.tidyverse.org/</a></p>
</div>
<div id="section-wrangling-dplyrsummarize" class="section level3">
<h3>Wrangling: dplyr::summarize</h3>
<p><strong>Summarize</strong></p>
<p><img src="images/dplyr.png" style="width:1in" /></p>
<p>Get a summary of selected variables with <code>summarize</code></p>
<div class="tutorial-exercise" data-label="wrangle2-summar-commute"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Create a summary of the participants_mutate data
# with the mean number of siblings
# and median years of study
summarize(participants_data,
mean(years_of_study),
median(letters_in_first_name))</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-summar-commute-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>summarize(participants_data,
mean(number_of_siblings),
median(years_of_study))</code></pre>
</div>
</div>
<div id="section-wrangling-magrittr-use" class="section level3">
<h3>Wrangling: magrittr use</h3>
<p><strong>Pipeline %>%</strong></p>
<ul>
<li>Do all the previous with a <code>magrittr</code> pipeline %>%.
Use the <code>group_by</code> function to get these results for
comparison between groups.</li>
</ul>
<div class="tutorial-exercise" data-label="wrangle2-pipe-long"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Use the magrittr pipe to summarize
# the mean days to email response,
# median letters in first name,
# and maximum years of study by gender
participants_data %>%
group_by(research_continent) %>%
summarize(mean(days_to_email_response),
median(letters_in_first_name),
max(years_of_study))</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-pipe-long-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>participants_data %>%
group_by(gender) %>%
summarize(mean(days_to_email_response),
median(letters_in_first_name),
max(years_of_study))</code></pre>
</div>
<p>Now use the <code>mutate</code> function to subset the data and use
the <code>group_by</code> function to get these results for comparisons
between groups.</p>
<div class="tutorial-exercise" data-label="wrangle2-pipe-long2"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Use the magrittr pipe to create a new column
# called commute, where those who travel
# more than 10km to get to the office
# are called "commuter" and others are "local".
# Summarize the mean days to email response,
# median letters in first name,
# and maximum years of study.
participants_data %>%
mutate(response_speed = ifelse(
days_to_email_response > 1,
"slow", "fast")) %>%
group_by(response_speed) %>%
summarize(mean(number_of_siblings),
median(years_of_study),
max(letters_in_first_name))</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-pipe-long2-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>participants_data %>%
mutate(commute = ifelse(
km_home_to_office > 10,
"commuter", "local")) %>%
group_by(commute) %>%
summarize(mean(days_to_email_response),
median(letters_in_first_name),
max(years_of_study))</code></pre>
</div>
</div>
<div id="section-purrr-apply-a-function-to-each-element-of-a-vector"
class="section level3">
<h3>purrr: Apply a function to each element of a vector</h3>
<p><img src="images/purrr.jpg" style="width:4in" /></p>
<p>We will use the <code>purrr</code> library to run a regression <span
class="citation">(Wickham and Henry 2023)</span>. Run the code
<code>library(purrr)</code> in your local R console to load the
library.</p>
<p><img src="images/purrr.jpg" style="width:1in" /> Now we will use the
<code>purrr</code> library for a simple linear regression <span
class="citation">(Wickham and Henry 2023)</span>. Note that when using
base R functions with the <code>magrittr</code> pipeline we use ‘.’ to
refer to the data. The functions <code>split</code> and <code>lm</code>
are from base R and stats <span class="citation">(R Core Team
2023)</span>.</p>
<p>Use purrr to solve: split a data frame into pieces, fit a model to
each piece, compute the summary, then extract the R^2.</p>
<div class="tutorial-exercise" data-label="wrangle2-purr-regression"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Split the data frame by batch,
# fit a linear model formula
# (days to email response as dependent
# and working hours as independent)
# to each batch, compute the summary,
# then extract the R^2.
participants_data %>%
split(.$gender) %>%
map(~
lm(number_of_publications ~
number_of_siblings,
data = .)) %>%
map(summary) %>%
map_dbl("r.squared")</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-purr-regression-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code> participants_data %>%
split(.$batch) %>% # from base R
map(~
lm(days_to_email_response ~
working_hours_per_day,
data = .)) %>%
map(summary) %>%
map_dbl("r.squared")</code></pre>
</div>
<p>Learn more about purrr from in <a
href="https://purrr.tidyverse.org/">the tidyverse</a> and from <a
href="http://varianceexplained.org/r/teach-tidyverse/">varianceexplained</a>.</p>
<p><a
href="https://github.com/rstudio/cheatsheets/blob/master/purrr.pdf">Check
out the purr Cheatsheet</a></p>
<p><img src="images/tidyR.png" style="width:1in" /> <img
src="images/dplyr.png" style="width:1in" /> <img
src="images/magrittr.png" style="width:1in" /> ### Test your new
skills</p>
<p><strong>Your turn to perform</strong></p>
<p>Up until this point the code has been provided for you to work on.
Now it is time for you to apply your new found skills. Please work
through the wrangling tasks we just went though. Use the
<code>diamonds</code> data and make the steps in long format
(i.e. assigning each step to an object) and short format with (i.e. with
the magrittr pipeline):</p>
<ul>
<li>select: carat and price</li>
<li>filter: only where carat is > 0.5</li>
<li>rename: rename price as cost</li>
<li>mutate: create a variable with ‘expensive’ if greater than mean of
cost and ‘cheap’ otherwise</li>
<li>group_by: split into cheap and expensive</li>
<li>summarize: give some summary statistics of your choice</li>
</ul>
<p>The diamonds data is built in with the <code>ggplot2</code> library.
It is already available in your R environment. Look at the help file
with <code>?diamonds</code> to learn more about it.</p>
<div class="tutorial-exercise" data-label="wrangle2-final"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="wrangle2-final-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code> diamonds %>%
# - select: carat and price
select(carat, price) %>%
# - filter: only where carat is > 0.5
filter(carat > 0.5) %>%
# - rename: rename price as cost
rename(cost = price) %>%
# - mutate: create a variable 'cheap_expensive' with 'expensive' if greater than mean of cost and 'cheap' otherwise
mutate(cheap_expensive = ifelse(
cost > mean(cost),
"expensive", "cheap")) %>%
# - group_by: split into cheap and expensive
group_by(cheap_expensive) %>%
# - summarize: give some summary statistics of your choice
summarize(mean(cost), mean(carat))</code></pre>
</div>
</div>
</div>
<div id="section-data-visualization" class="section level2">
<h2>Data Visualization</h2>
<p><img src="images/Circos.png" style="width:1.5in" /> <img
src="images/ggplot2.png" style="width:1.1in" /></p>
<p>The video below offers an overview of the content in covered in this
section of the tutorial. Keep in mind that I did not yet have your data
when I recorded this so the results of my scripts may be slightly
different than yours. Feel free to watch the video and follow along or
simply work through the tutorial. All the <a
href="http://htmlpreview.github.io/?https://github.com/CWWhitney/teaching_R/blob/master/Data_Visualization_Rpres/Data_Visualization.html#/">Data
Visualization slides</a> are also available as a web page.</p>
<!-- ![](https://youtu.be/y4BPnwyxU-E) -->
<iframe width="560" height="315" src="https://www.youtube.com/embed/y4BPnwyxU-E" frameborder="0" allowfullscreen>
</iframe>
<div id="section-getting-stuck" class="section level3">
<h3>Getting stuck</h3>
<p>If you ever get stuck</p>
<ul>
<li><strong>Open RStudio</strong></li>
</ul>
<p><img src="images/rstudio-hex.png" style="width:1in" /></p>
<ul>
<li>Type <code>?</code> in R console with function, package or data
name</li>
<li>Add <code>R</code> to an internet search query with a copy of any
error message you get from R</li>
<li>In RStudio on your machine find <strong>Help > Cheatsheets >
Data Visualization with ggplot2</strong></li>
</ul>
<p>For getting help</p>
<ul>
<li>Many talented programmers</li>
<li>Some scan the web and answer issues</li>
</ul>
<p><img src="images/stack-overflow.png" style="width:4in" /></p>
<p><a href="https://stackoverflow.com"
class="uri">https://stackoverflow.com</a></p>
<p><img src="images/hadley_wickham.png" style="width:1in" /></p>
<p>Hadley Wickham</p>
<p><img src="images/Yihui.png" style="width:1in" /></p>
<p>Yihui Xie <a href="https://yihui.name/en/2017/08/so-gh-email"
class="uri">https://yihui.name/en/2017/08/so-gh-email</a></p>
<p><a href="https://rmarkdown.rstudio.com/"
class="uri">https://rmarkdown.rstudio.com/</a></p>
</div>
<div id="section-getting-your-data-in-r-1" class="section level3">
<h3>Getting your data in R</h3>
<p><img src="images/R_logo.png" style="width:1in" /></p>
<ul>
<li>Load the data with the <code>read.csv</code> function</li>
</ul>
<pre><code>participants_data <- read.csv("participants_data.csv")</code></pre>
<ul>
<li>Keep your data in the same folder structure as the .RProj file</li>
<li>at or below the level of the .RProj file</li>
</ul>
</div>
<div id="section-creating-a-barplot-in-base-r" class="section level3">
<h3>Creating a barplot in base R</h3>
<p><strong>R has several systems for making graphs</strong></p>
<ul>
<li><strong>Base R</strong></li>
<li>Create a barplot with the <code>table()</code> and
<code>barplot()</code> functions</li>
</ul>
<div class="tutorial-exercise" data-label="vis-base-barplot"
data-completion="1" data-diagnostics="1" data-startover="1"