-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.html
4561 lines (2651 loc) · 105 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 lang="" xml:lang="">
<head>
<title>A {fun} Intro to R Programming</title>
<meta charset="utf-8" />
<script src="libs/header-attrs/header-attrs.js"></script>
<link href="libs/remark-css/default.css" rel="stylesheet" />
<meta name="description" content="Intro to R Programming"/>
<meta name="generator" content="xaringan and remark.js"/>
<meta name="github-repo" content="favstats/xxx"/>
<meta name="twitter:title" content="Intro to R Programming"/>
<meta name="twitter:description" content="Intro to R Programming"/>
<meta name="twitter:url" content="https://www.favstats.eu"/>
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:creator" content="@favstats"/>
<meta name="twitter:site" content="@favstats"/>
<meta property="og:title" content="Intro to R Programming"/>
<meta property="og:description" content="Intro to R Programming"/>
<meta property="og:url" content="https://www.favstats.eu"/>
<meta property="og:type" content="website"/>
<meta property="og:locale" content="en_US"/>
<meta property="article:author" content="Fabio Votta"/>
<link href="libs/tile-view/tile-view.css" rel="stylesheet" />
<script src="libs/tile-view/tile-view.js"></script>
<link href="libs/panelset/panelset.css" rel="stylesheet" />
<script src="libs/panelset/panelset.js"></script>
<script src="libs/clipboard/clipboard.min.js"></script>
<link href="libs/xaringanExtra-clipboard/xaringanExtra-clipboard.css" rel="stylesheet" />
<script src="libs/xaringanExtra-clipboard/xaringanExtra-clipboard.js"></script>
<script>window.xaringanExtraClipboard(null, {"button":"Copy Code","success":"Copied!","error":"Press Ctrl+C to Copy"})</script>
<link href="libs/xaringanExtra-extra-styles/xaringanExtra-extra-styles.css" rel="stylesheet" />
<link rel="stylesheet" href="css/lexis.css" type="text/css" />
<link rel="stylesheet" href="css/lexis-fonts.css" type="text/css" />
</head>
<body>
<textarea id="source">
layout: true
<style>
.onehundredtwenty {
font-size: 120%;
}
<style>
.ninety {
font-size: 90%;
}
.eightyfive {
font-size: 85%;
}
.eighty {
font-size: 80%;
}
.seventyfive {
font-size: 75%;
}
.seventy {
font-size: 70%;
}
.fifty {
font-size: 50%;
}
.forty {
font-size: 40%;
}
</style>
---
class: banner
---
name: title-slide
class: primary
#.fancy[A {fun} Intro to R Programming]
###.fancy[The basics,<br>data wrangling,<br>and more!]
<br>
Fabio Votta
[<svg aria-hidden="true" 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:black;overflow:visible;position:relative;"><path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"/></svg> @favstats](http://twitter.com/favstats)<br>
[<svg aria-hidden="true" role="img" viewBox="0 0 496 512" style="height:1em;width:0.97em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:black;overflow:visible;position:relative;"><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg> @favstats](http://github.com/favstats)<br>
[<svg aria-hidden="true" role="img" viewBox="0 0 640 512" style="height:1em;width:1.25em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:black;overflow:visible;position:relative;"><path d="M579.8 267.7c56.5-56.5 56.5-148 0-204.5c-50-50-128.8-56.5-186.3-15.4l-1.6 1.1c-14.4 10.3-17.7 30.3-7.4 44.6s30.3 17.7 44.6 7.4l1.6-1.1c32.1-22.9 76-19.3 103.8 8.6c31.5 31.5 31.5 82.5 0 114L422.3 334.8c-31.5 31.5-82.5 31.5-114 0c-27.9-27.9-31.5-71.8-8.6-103.8l1.1-1.6c10.3-14.4 6.9-34.4-7.4-44.6s-34.4-6.9-44.6 7.4l-1.1 1.6C206.5 251.2 213 330 263 380c56.5 56.5 148 56.5 204.5 0L579.8 267.7zM60.2 244.3c-56.5 56.5-56.5 148 0 204.5c50 50 128.8 56.5 186.3 15.4l1.6-1.1c14.4-10.3 17.7-30.3 7.4-44.6s-30.3-17.7-44.6-7.4l-1.6 1.1c-32.1 22.9-76 19.3-103.8-8.6C74 372 74 321 105.5 289.5L217.7 177.2c31.5-31.5 82.5-31.5 114 0c27.9 27.9 31.5 71.8 8.6 103.9l-1.1 1.6c-10.3 14.4-6.9 34.4 7.4 44.6s34.4 6.9 44.6-7.4l1.1-1.6C433.5 260.8 427 182 377 132c-56.5-56.5-148-56.5-204.5 0L60.2 244.3z"/></svg> favstats.eu](https://www.favstats.eu)
August 14 2023
.fifty[Link to slides: [favstats.github.io/ds3_r_intro](https://favstats.github.io/ds3_r_intro)]
---
### Your friendly neighborhood R Instructor
.leftcol40[
<img src="https://github.com/favstats/WarwickSpringCamp_QTA/blob/main/docs/slides/day1/images/me.jpg?raw=true" style="width: 90%" />
]
.rightcol60[
+ Ph.D. Candidate in Political Communication at University of Amsterdam
+ Passionate about R and Data Science
+ I love to travel
+ I enjoy and (occasionally) create R memes
<center>
<img src="https://pbs.twimg.com/profile_banners/866598352577867776/1495448334/1500x500" width="60%">
.font60[
[twitter.com/rstatsmemes](https://twitter.com/rstatsmemes)
</center>
]
]
---
class: middle, center
## But enough of me
### Let's learn something about .blue[you]!
<center>
<img src="images/mentimeter_qr_code.png" width="20%">
</center>
Go to `menti.com` and type in the code: 6410 5559
or visit this website: [menti.com/aloxtiea8mx3](https://www.menti.com/aloxtiea8mx3)
---
<div style='position: relative; padding-bottom: 56.25%; padding-top: 35px; height: 0; overflow: hidden;'><iframe sandbox='allow-scripts allow-same-origin allow-presentation' allowfullscreen='true' allowtransparency='true' frameborder='0' height='315' src='https://www.mentimeter.com/app/presentation/al367t83vwruzc9sy2tu65mrpwjfqqtt/embed' style='position: absolute; top: 0; left: 0; width: 100%; height: 100%;' width='420'></iframe></div>
---
### It's not unusual to struggle at first but it gets better!
<img src="images/r_first_then_new.png" width="80%" style="display: block; margin: auto;" />
<!-- ![](images/r_first_then_new.png){width=50%} -->
.fifty[Illustration adapted from [Allison Horst](https://twitter.com/allison_horst)]
--
+ My experience is that this stuff isn't super easy... but it gets better!
--
+ Awesome inclusive community that is always ready to help
+ Active blogosphere with use cases and examples
---
### A Note on Live-Coding
<img src="https://cdn.myportfolio.com/45214904-6a61-4e23-98d6-b140f8654a40/118ae091-4329-4382-8cb8-012035593dfc_rw_1920.png?h=29e95985575f458232d53ad93d1860cb" width="80%" style="display: block; margin: auto;" />
---
## Overview
+ R Basics
+ Operators
+ Objects (inc. vectors)
+ Functions
+ Exercises
+ Data frames
`\(\text{B R E A K at 2pm (CET) - (5:30 PM IST) - (8:00 PM CST)}\)`
+ Data Manipulation
+ the tidyverse and friends
+ `janitor`
+ `tidyr`
+ `dplyr`
+ Exercises
---
<!-- #### What is <img src="images/Rlogo.svg" width="30px" inline-block/> ? -->
#### What is <img src="images/Rlogo.svg" style="display: inline-block; margin: 0"; width="30px"/>?
R is a .fancy[statistical] programming language developed for data analysis and visualization.
#### What is <img src="images/rstudio.png" style="display: inline-block; margin: 0"; width="80px"/>?
RStudio is an IDE (Integrated Development Environment).
* Write, save and open R Code (.R/.Rmd files)
* Provides syntax-highlighting and auto-completion & much more
--
# But why learn <img src="images/Rlogo.svg" style="display: inline-block; margin: 0"; width="60px"/>?
<!-- ![](http://www.favstats.eu/img/headers/season_views.png) -->
---
<!-- #### What is <img src="images/Rlogo.svg" width="30px" inline-block/> ? -->
.leftcol[
#### Why learn <img src="images/Rlogo.svg" style="display: inline-block; margin: 0"; width="30px"/>?
<!-- ![](http://www.favstats.eu/img/headers/season_views.png) -->
+ Amazing Community .forty[(but I already said that)]
<!-- + Outstanding repertoire of statistical & computational methods -->
<!-- -- -->
<!-- + Integrates well with other programming languages (like Python) -->
<!-- -- -->
<!-- + Beautiful Data Visualization with `ggplot2` and more -->
]
.rightcol[
![](https://raw.githubusercontent.com/allisonhorst/stats-illustrations/main/rstats-artwork/welcome_to_rstats_twitter.png)
.fifty[Artist: [Allison Horst](https://github.com/allisonhorst)]
]
---
<!-- #### What is <img src="images/Rlogo.svg" width="30px" inline-block/> ? -->
.leftcol60[
#### Why learn <img src="images/Rlogo.svg" style="display: inline-block; margin: 0"; width="30px"/>?
<!-- ![](http://www.favstats.eu/img/headers/season_views.png) -->
+ Amazing Community .forty[(but I already said that)]
+ Data wrangling is accessible & fun
<!-- + Outstanding repertoire of statistical & computational methods -->
<!-- -- -->
<!-- + Integrates well with other programming languages (like Python) -->
<!-- -- -->
<!-- + Beautiful Data Visualization with `ggplot2` and more -->
]
.rightcol40[
<br>
![](https://github.com/allisonhorst/stats-illustrations/blob/main/rstats-artwork/tidyverse_celestial.png?raw=true)
.fifty[Artist: [Allison Horst](https://github.com/allisonhorst)]
]
---
.leftcol60[
#### Why learn <img src="images/Rlogo.svg" style="display: inline-block; margin: 0"; width="30px"/>?
<!-- ![](http://www.favstats.eu/img/headers/season_views.png) -->
+ Amazing Community .forty[(but I already said that)]
+ Data wrangling is accessible & fun
+ Outstanding repertoire of statistical & computational methods
<!-- + Integrates well with other programming languages (like Python) -->
<!-- + Beautiful Data Visualization with `ggplot2` and more -->
]
.rightcol40[
<br>
![](https://pbs.twimg.com/media/E4mDfxSXEAA_kvG.jpg)
.fifty[ [easystats](https://github.com/easystats/easystats) packageverse for statistical analysis.]
]
---
.leftcol60[
#### Why learn <img src="images/Rlogo.svg" style="display: inline-block; margin: 0"; width="30px"/>?
<!-- ![](http://www.favstats.eu/img/headers/season_views.png) -->
+ Amazing Community .forty[(but I already said that)]
+ Data wrangling is accessible & fun
+ Outstanding repertoire of statistical & computational methods
+ Integrates well with other programming languages
<!-- + Beautiful Data Visualization with `ggplot2` and more -->
]
.rightcol40[
<br>
![](https://rstudio.github.io/reticulate/images/reticulated_python.png)
.fifty[ [reticulate](https://github.com/easystats/easystats) integrates Python into R]
]
---
.leftcol60[
#### Why learn <img src="images/Rlogo.svg" style="display: inline-block; margin: 0"; width="30px"/>?
<!-- ![](http://www.favstats.eu/img/headers/season_views.png) -->
+ Amazing Community .forty[(but I already said that)]
+ Data wrangling is accessible & fun
+ Outstanding repertoire of statistical & computational methods
+ Integrates well with other programming languages
+ Beautiful data visualization with `ggplot2`
<br>
<br>
.right[.fifty[Data visualization by [Cédric Scherer](https://www.cedricscherer.com/)]]
]
.rightcol40[
![](https://raw.githubusercontent.com/Z3tt/TidyTuesday/master/plots/2020_31/2020_31_PalmerPenguins.png?raw=true)
]
---
.leftcol75[
### This workshop is held in .green[Rmarkdown]
]
.rightcol25[
<img src="https://rmarkdown.rstudio.com/docs/reference/figures/logo.png" width="80" height="100" style="display: block; margin: auto 0 auto auto;" />
]
<center>
<img src="https://github.com/allisonhorst/stats-illustrations/blob/main/rstats-artwork/rmarkdown_wizards.png?raw=true" style="width: 76%" />
</center>
.fifty[Artist: [Allison Horst](https://github.com/allisonhorst)]
---
.leftcol75[
## .green[Rmarkdown]
]
.rightcol25[
<img src="https://rmarkdown.rstudio.com/docs/reference/figures/logo.png" width="100" height="120" style="display: block; margin: auto 0 auto auto;" />
]
.leftcol[
<center>
<img src="https://github.com/allisonhorst/stats-illustrations/blob/main/rstats-artwork/rmarkdown_wizards.png?raw=true" style="width: 100%" />
</center>
.fifty[Artist: [Allison Horst](https://github.com/allisonhorst)]
]
.rightcol[
* create *documents* with R
* mix code, text and graphs as you like
* write your thesis, create slides, automated reports, dashboards and interactive web apps all from within [Rmarkdown](https://rmarkdown.rstudio.com/docs/articles/rmarkdown.html)
]
---
## R vs. Rmarkdown
.seventy[
There are two (main) ways you can create scripts in R
]
.pull-left[
.seventy[
R scripts (file ending `.R`):
]
![](images/rcode.png)
]
.pull-right[
.seventy[
Rmarkdown (`.Rmd`) scripts produce HTML:
]
<img src="images/rmarkown_html.png" style="width: 72%" />
]
---
## R vs. Rmarkdown
.seventy[
There are two (main) ways you can create scripts in R
]
.pull-left[
.seventy[
R scripts (file ending `.R`):
]
![](images/rcode.png)
]
.pull-right[
.seventy[
Rmarkdown (`.Rmd`) scripts produce PDF:
]
![](images/rmarkown_pdf.png)
]
---
## R vs. Rmarkdown
.seventy[
There are two (main) ways you can create scripts in R
]
.pull-left[
.seventy[
R scripts (file ending `.R`):
]
![](images/rcode.png)
]
.pull-right[
.seventy[
Rmarkdown (`.Rmd`) scripts produce so much more:
]
slides (*like the ones you are looking at right now!*), automated reports, dashboards and interactive web apps
]
---
## We'll switch to RStudio in a moment
> The next few slides will have a couple of links that you may want to follow. You can find **all** materials of this workshop, including links, in this GitHub repository: [tinyurl.com/ds3repo](https://github.com/favstats/ds3_r_intro)
.font80[
Hopefully at this point you have already followed the **pre-workshop instructions:**
[tinyurl.com/ds3prep](https://favstats.github.io/ds3_r_intro/prep/instructions.html)
*Once in RStudio, feel free to code along with me, changing a few bits here and there and see how results change.*
]
---
## Alternatives to local RStudio
If you cannot install R or RStudio on your computer for any reason, you have two options to follow along:
+ **Binder**
Simply start a **Binder** instance which will create a session of RStudio in your Browser (may take a little bit):
[tinyurl.com/ds3binder](https://tinyurl.com/ds3binder)
[![Binder](https://img.shields.io/badge/launch-binder-579aca.svg?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFkAAABZCAMAAABi1XidAAAB8lBMVEX///9XmsrmZYH1olJXmsr1olJXmsrmZYH1olJXmsr1olJXmsrmZYH1olL1olJXmsr1ol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i8XpXC4V3t7/ADuTNKaQJdScAAAAAElFTkSuQmCC)](https://mybinder.org/v2/gh/favstats/ds3_r_intro/rstudio?urlpath=rstudio)
> Note: If you are using Binder don't forget to download the files before you close out the session because otherwise anything you added will be lost!
---
## Alternatives to local RStudio
+ **Google Colab**
Google Colab instantaneously runs Jupyter Notebooks in your browser with an R Kernel.
+ [Part I (R Basics): tinyurl.com/ds3rintro1](https://colab.research.google.com/drive/1dLsdGbkvgn1JbWgsy9Z-pFmPd_2MG4Xu?usp=sharing)
+ [Part II (Data Manipulation with the `tidyverse`): tinyurl.com/ds3rintro2](https://colab.research.google.com/drive/14CRElnKewnp5MnlxhqVu6OOcIXd-Bkaj?usp=sharing)
---
## Workshop files
Please download the workshop files from here: [https://tinyurl.com/ds3files](https://www.dropbox.com/sh/jievqgwl43nwnbf/AADhoYQW5oMZ-JygK7aNklHra?dl=0).
The link will download a `.zip` file. Extract it to **its own folder**.
Now double-click on **ds3_intro.Rproj**
![](images/click_on_rproject.png)
---
## Within RStudio
![](images/rstudio_instructions.png)
<!-- ## RStudio Tour -->
<!-- <center> -->
<!-- <img src="https://bookdown.org/ageraci/STAT160Companion/images/rstudiopanes.png" style="width: 82%" /> -->
<!-- </center> -->
<!-- --- -->
<!-- ## RStudio Tour -->
<!-- * Program pane -->
<!-- * Shows R Files, typically .R or .Rmd -->
<!-- * Environment pane -->
<!-- * All the objects that you created during your session -->
<!-- * Console pane -->
<!-- * For executing short snippets of R code -->
<!-- * Files pane -->
<!-- * Gives access to files, but also help, and plots -->
<!-- --- -->
---
class: inverse, middle, center
# R Basics
--
### Math Operators
---
### Math Operators
At its core R is just a fancy *calculator*
You can do:
`+` addition
`-` subtraction
`*` multiplication
`/` division
`^` exponentiate
---
### Math Operators
At its core R is just a fancy *calculator*
### `+` addition
.details[
```r
15 + 5
```
```
#> [1] 20
```
]
--
### Mixing operators
.details[
```r
(15 + 5) / (2 * 5)
```
```
#> [1] 2
```
]
---
## Short excourse: a new data set appears..
But adding up numbers for no reason is no fun.
--
That's why we will use a data set about .fancy[animals] to learn some R Basics.
.leftcol45[
<img src="images/kisspng-animal-clip-art-portable-network-graphics-illustra-cartoon-animal-transparent-amp-png-clipart-free-5c7ccdbb1c9c98.7009291415516830031172.png" style="width: 100%" />
]
.rightcol55[
[Animal Ageing and Longevity Database](https://www.johnsnowlabs.com/marketplace/the-animal-aging-and-longevity-database/)
Data on over 4200 animals.
Information on age of maturity, gestation or incubation periods but also **longevity (in years)**.
]
---
## Animal Ageing and Longevity Database
Say we want to know how old an animal is in *human years*.
--
We can use the following simple formula to determine that:
<br>
`$$\frac{\text{Maximum lifespan human}}{\text{Maximum lifespan non-human animal}} = \text{animal to human years ratio}$$`
<br>
*Note: This is just a **very rough** way to determine the conversion ratio. It is **much more** complicated in [reality](https://www.akc.org/expert-advice/health/how-to-calculate-dog-years-to-human-years/).*
---
## Animal Ageing and Longevity Database
.leftcol[
| Animal | Maximum Lifespan |
| --- | --- |
| Human | 122.5 |
| Domestic dog | 24.0 |
| Domestic cat | 30.0 |
| American alligator | 77.0 |
| Golden hamster | 3.9 |
| King penguin | 26.0 |
| Lion| 27.0 |
| Greenland shark | 392.0 |
| Galapagos tortoise | 177.0 |
]
.rightcol[
| Animal | Maximum Lifespan |
| --- | --- |
| African bush elephant | 65.0 |
| California sea lion | 35.7 |
| Fruit fly | 0.3 |
| House mouse | 4.0 |
| Giraffe | 39.5 |
| Wild boar | 27.0 |
Source: [Animal Ageing and Longevity Database](https://www.johnsnowlabs.com/marketplace/the-animal-aging-and-longevity-database/)
]
---
### Math Operators
Say we want to know how old a dog is in *human years*.
--
The observed maximum lifespan of a human is 122.5 years. For dogs it is 24.
--
.details[
```r
122.5/24
```
```
#> [1] 5.104167
```
]
So for every year a human ages, a dog "ages" 5.1 human years. How old is a 15 year old dog in human years?
.details[
```r
5.104167*15
```
```
#> [1] 76.56251
```
]
---
## So many numbers..
Now it can be quite tedious to juggle all those numbers around.
Especially if we want to keep reusing numbers we calculated before.
--
Here to simplify that process are:
**Objects**
---
class: inverse, middle, center
# R Basics
### R Objects
---
## R Objects
You can think of R objects as *saving information*, for example simple numbers or just plain text.
Once saved we can recall it whenever we want by just running the name of the object.
--
> Everything that exists in R is *.red[an object]*. .fifty[~John M. Chambers]
--
We create R objects by using the assignment operator:
<center>
.large[**`<-`**]
</center>
.right[
.fifty[You can also assign with the **`=`** sign but we will not be using this here.]
]
---
## R Objects
Here is an example:
```r
human_lifespan <- 122.5
dog_lifespan <- 24
```
If we now run the respective objects we retrieve the saved numbers.
.details[
```r
human_lifespan
```
```
#> [1] 122.5
```
]
.details[
```r
dog_lifespan
```
```
#> [1] 24
```
]
---
## R Objects
Now we can perform the same calculation as before but this time using objects!
```r
dogs_to_human <- human_lifespan / dog_lifespan
```
The object `dogs_to_human` now holds the dog to human years conversion ratio.
--
*Now* we ask again: how old is a 15 year old dog in human years?
.details[
```r
dogs_to_human*15
```
```
#> [1] 76.5625
```
]
---