-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1757 lines (1261 loc) · 58 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>Introduction to the Tidyverse</title>
<meta charset="utf-8" />
<meta name="author" content="Olivier Gimenez" />
<script src="libs/header-attrs-2.11/header-attrs.js"></script>
<link href="libs/font-awesome-5.15.3/css/all.min.css" rel="stylesheet" />
<link href="libs/font-awesome-5.15.3/css/v4-shims.min.css" rel="stylesheet" />
<link rel="stylesheet" href="css/rutgers-tidyverse.css" type="text/css" />
<link rel="stylesheet" href="css/rutgers-fonts_og.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: center, middle, inverse, title-slide
# Introduction to the Tidyverse
## How to be a tidy data scientist
### Olivier Gimenez
### Novembre 2021
---
# **Tidyverse**
- **Ordocosme** in 🇫🇷 with _Tidy_ for "bien rangé" and _verse_ for "univers"
- A collection of R 📦 developed by H. Wickham and others at Rstudio
<img src="img/wickham_president.jpg" width="50%" style="display: block; margin: auto;" />
---
# **Tidyverse**
* "A framework for managing data that aims at making the cleaning and preparing steps [muuuuuuuch] easier" (Julien Barnier).
* Main characteristics of a tidy dataset:
- each variable is a column
- each observation is a raw
- each value is in a different cell
<img src="img/tidydata.png" width="80%" style="display: block; margin: auto;" />
---
# **Tidyverse** is a collection of R 📦
* `ggplot2` - visualising stuff
* `dplyr`, `tidyr` - data manipulation
* `purrr` - advanced programming
* `readr` - import data
* `tibble` - improved data.frame format
* `forcats` - working w/ factors
* `stringr` - working w/ chain of characters
---
# **Tidyverse** is a collection of R 📦
* [`ggplot2` - visualising stuff](https://ggplot2.tidyverse.org/)
* [`dplyr`, `tidyr` - data manipulation](https://dplyr.tidyverse.org/)
* `purrr` - advanced programming
* [`readr` - import data](https://readr.tidyverse.org/)
* [`tibble` - improved data.frame format](https://tibble.tidyverse.org/)
* [`forcats` - working w/ factors](https://forcats.tidyverse.org/)
* [`stringr` - working w/ chain of characters](https://stringr.tidyverse.org/)
---
class: middle
# Workflow in data science
<img src="img/data-science-workflow.png" width="100%" style="display: block; margin: auto;" />
---
class: middle
# Workflow in data science, with **Tidyverse**
<img src="img/01_tidyverse_data_science.png" width="90%" style="display: block; margin: auto;" />
---
background-image: url(https://github.com/rstudio/hex-stickers/raw/master/SVG/tidyverse.svg?sanitize=true)
background-size: 100px
background-position: 90% 3%
# Load [tidyverse](www.tidyverse.org)&nbsp;📦
```r
# install.packages("tidyverse")
library(tidyverse)
```
---
class: middle
## Case study:
# [Using Twitter to predict citation rates of ecological research](https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0166570)
<img src="img/paper_workflow.png" width="85%" style="display: block; margin: auto;" />
---
class: inverse, center, middle
# Import
---
# Import data
**readr::read_csv** function:
* ~~keeps input types as is (no conversion to factor)~~ (since `R` 4.0.0)
* creates `tibbles` instead of `data.frame`
- no names to rows
- allows column names with special characters (see next slide)
- more clever on screen display than w/ data.frames (see next slide)
- [no partial matching on column names](https://stackoverflow.com/questions/58513997/how-to-make-r-stop-accepting-partial-matches-for-column-names)
- warning if attempt to access unexisting column
* is daaaaaamn fast 🏎
---
# Import data
```r
citations_raw <- readr::read_csv('https://raw.githubusercontent.com/oliviergimenez/intro_tidyverse/master/journal.pone.0166570.s001.CSV')
citations_raw
```
```
## # A tibble: 1,599 × 12
## `Journal identity` `5-year journal im… `Year published` Volume Issue Authors `Collection dat… `Publication da…
## <chr> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr>
## 1 Ecology Letters 16.7 2014 17 12 Morin et … 2/1/2016 9/16/2014
## 2 Ecology Letters 16.7 2014 17 12 Jucker et… 2/1/2016 10/13/2014
## 3 Ecology Letters 16.7 2014 17 12 Calcagno … 2/1/2016 10/21/2014
## 4 Ecology Letters 16.7 2014 17 11 Segre et … 2/1/2016 8/28/2014
## 5 Ecology Letters 16.7 2014 17 11 Kaufman e… 2/1/2016 8/28/2014
## 6 Ecology Letters 16.7 2014 17 10 Nasto et … 2/2/2016 7/28/2014
## 7 Ecology Letters 16.7 2014 17 10 Tschirren… 2/2/2016 8/6/2014
## 8 Ecology Letters 16.7 2014 17 9 Barnechi … 2/2/2016 6/17/2014
## 9 Ecology Letters 16.7 2014 17 9 Pinto-San… 2/2/2016 6/12/2014
## 10 Ecology Letters 16.7 2014 17 9 Clough et… 2/2/2016 7/17/2014
## # … with 1,589 more rows, and 4 more variables: Number of tweets <dbl>, Number of users <dbl>, Twitter reach <dbl>,
## # Number of Web of Science citations <dbl>
```
---
class: inverse, center, middle
# Tidy, transform
---
# Rename columns
```r
citations_temp <- dplyr::rename(citations_raw,
journal = 'Journal identity',
impactfactor = '5-year journal impact factor',
pubyear = 'Year published',
colldate = 'Collection date',
pubdate = 'Publication date',
nbtweets = 'Number of tweets',
woscitations = 'Number of Web of Science citations')
citations_temp
```
```
## # A tibble: 1,599 × 12
## journal impactfactor pubyear Volume Issue Authors colldate pubdate nbtweets `Number of user… `Twitter reach`
## <chr> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
## 1 Ecology … 16.7 2014 17 12 Morin et … 2/1/2016 9/16/2… 18 16 29877
## 2 Ecology … 16.7 2014 17 12 Jucker et… 2/1/2016 10/13/… 15 12 5997
## 3 Ecology … 16.7 2014 17 12 Calcagno … 2/1/2016 10/21/… 5 4 1667
## 4 Ecology … 16.7 2014 17 11 Segre et … 2/1/2016 8/28/2… 9 8 3482
## 5 Ecology … 16.7 2014 17 11 Kaufman e… 2/1/2016 8/28/2… 3 3 1329
## 6 Ecology … 16.7 2014 17 10 Nasto et … 2/2/2016 7/28/2… 27 23 41906
## 7 Ecology … 16.7 2014 17 10 Tschirren… 2/2/2016 8/6/20… 6 6 12223
## 8 Ecology … 16.7 2014 17 9 Barnechi … 2/2/2016 6/17/2… 19 18 22020
## 9 Ecology … 16.7 2014 17 9 Pinto-San… 2/2/2016 6/12/2… 26 23 23003
## 10 Ecology … 16.7 2014 17 9 Clough et… 2/2/2016 7/17/2… 44 42 131788
## # … with 1,589 more rows, and 1 more variable: woscitations <dbl>
```
---
# Create (or modify) columns
```r
citations <- dplyr::mutate(citations_temp, journal = as.factor(journal))
citations
```
```
## # A tibble: 1,599 × 12
## journal impactfactor pubyear Volume Issue Authors colldate pubdate nbtweets `Number of user… `Twitter reach`
## <fct> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
## 1 Ecology … 16.7 2014 17 12 Morin et … 2/1/2016 9/16/2… 18 16 29877
## 2 Ecology … 16.7 2014 17 12 Jucker et… 2/1/2016 10/13/… 15 12 5997
## 3 Ecology … 16.7 2014 17 12 Calcagno … 2/1/2016 10/21/… 5 4 1667
## 4 Ecology … 16.7 2014 17 11 Segre et … 2/1/2016 8/28/2… 9 8 3482
## 5 Ecology … 16.7 2014 17 11 Kaufman e… 2/1/2016 8/28/2… 3 3 1329
## 6 Ecology … 16.7 2014 17 10 Nasto et … 2/2/2016 7/28/2… 27 23 41906
## 7 Ecology … 16.7 2014 17 10 Tschirren… 2/2/2016 8/6/20… 6 6 12223
## 8 Ecology … 16.7 2014 17 9 Barnechi … 2/2/2016 6/17/2… 19 18 22020
## 9 Ecology … 16.7 2014 17 9 Pinto-San… 2/2/2016 6/12/2… 26 23 23003
## 10 Ecology … 16.7 2014 17 9 Clough et… 2/2/2016 7/17/2… 44 42 131788
## # … with 1,589 more rows, and 1 more variable: woscitations <dbl>
```
---
# Create (or modify) columns
```r
levels(citations$journal)
```
```
## [1] "Animal Conservation" "Conservation Letters" "Diversity and Distributions"
## [4] "Ecological Applications" "Ecology" "Ecology Letters"
## [7] "Evolution" "Evolutionary Applications" "Fish and Fisheries"
## [10] "Functional Ecology" "Global Change Biology" "Global Ecology and Biogeography"
## [13] "Journal of Animal Ecology" "Journal of Applied Ecology" "Journal of Biogeography"
## [16] "Limnology and Oceanography" "Mammal Review" "Methods in Ecology and Evolution"
## [19] "Molecular Ecology Resources" "New Phytologist"
```
---
class: inverse, center, middle
# Give your code some air
---
# Cleaner code with "pipe" operator `%>%`
```r
citations_raw %>%
dplyr::rename(
journal = 'Journal identity',
impactfactor = '5-year journal impact factor',
pubyear = 'Year published',
colldate = 'Collection date',
pubdate = 'Publication date',
nbtweets = 'Number of tweets',
woscitations = 'Number of Web of Science citations') %>%
dplyr::mutate(journal = as.factor(journal))
```
```
## # A tibble: 1,599 × 12
## journal impactfactor pubyear Volume Issue Authors colldate pubdate nbtweets `Number of user… `Twitter reach`
## <fct> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
## 1 Ecology … 16.7 2014 17 12 Morin et … 2/1/2016 9/16/2… 18 16 29877
## 2 Ecology … 16.7 2014 17 12 Jucker et… 2/1/2016 10/13/… 15 12 5997
## 3 Ecology … 16.7 2014 17 12 Calcagno … 2/1/2016 10/21/… 5 4 1667
## 4 Ecology … 16.7 2014 17 11 Segre et … 2/1/2016 8/28/2… 9 8 3482
## 5 Ecology … 16.7 2014 17 11 Kaufman e… 2/1/2016 8/28/2… 3 3 1329
## 6 Ecology … 16.7 2014 17 10 Nasto et … 2/2/2016 7/28/2… 27 23 41906
## 7 Ecology … 16.7 2014 17 10 Tschirren… 2/2/2016 8/6/20… 6 6 12223
## 8 Ecology … 16.7 2014 17 9 Barnechi … 2/2/2016 6/17/2… 19 18 22020
## 9 Ecology … 16.7 2014 17 9 Pinto-San… 2/2/2016 6/12/2… 26 23 23003
## 10 Ecology … 16.7 2014 17 9 Clough et… 2/2/2016 7/17/2… 44 42 131788
## # … with 1,589 more rows, and 1 more variable: woscitations <dbl>
```
---
# Name object
```r
*citations <- citations_raw %>%
dplyr::rename(
journal = 'Journal identity',
impactfactor = '5-year journal impact factor',
pubyear = 'Year published',
colldate = 'Collection date',
pubdate = 'Publication date',
nbtweets = 'Number of tweets',
woscitations = 'Number of Web of Science citations') %>%
dplyr::mutate(journal = as.factor(journal))
```
---
# Syntax with pipe
* Verb(Subject,Complement) replaced by Subject %>% Verb(Complement)
* No need to name unimportant intermediate variables
* Clear syntax (readability)
<img src="img/logo_pipe.png" width="40%" style="display: block; margin: auto;" />
---
# Base R from [Lise Vaudor's blog](http://perso.ens-lyon.fr/lise.vaudor/)
```r
white_and_yolk <- crack(egg, add_seasoning)
omelette_batter <- beat(white_and_yolk)
omelette_with_chives <- cook(omelette_batter,add_chives)
```
<img src="img/piping_successive.jpg" width="500px" style="display: block; margin: auto;" />
---
# Piping from [Lise Vaudor's blog](http://perso.ens-lyon.fr/lise.vaudor/)
```r
egg %>%
crack(add_seasoning) %>%
beat() %>%
cook(add_chives) -> omelette_with_chives
```
<img src="img/piping_piped.png" width="250px" style="display: block; margin: auto;" />
---
class: inverse, center, middle
# Tidy, transform
---
# Select columns
```r
citations %>%
dplyr::select(journal, impactfactor, nbtweets)
```
```
## # A tibble: 1,599 × 3
## journal impactfactor nbtweets
## <fct> <dbl> <dbl>
## 1 Ecology Letters 16.7 18
## 2 Ecology Letters 16.7 15
## 3 Ecology Letters 16.7 5
## 4 Ecology Letters 16.7 9
## 5 Ecology Letters 16.7 3
## 6 Ecology Letters 16.7 27
## 7 Ecology Letters 16.7 6
## 8 Ecology Letters 16.7 19
## 9 Ecology Letters 16.7 26
## 10 Ecology Letters 16.7 44
## # … with 1,589 more rows
```
---
# Drop columns
```r
citations %>%
dplyr::select(-Volume, -Issue, -Authors)
```
```
## # A tibble: 1,599 × 9
## journal impactfactor pubyear colldate pubdate nbtweets `Number of users` `Twitter reach` woscitations
## <fct> <dbl> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Ecology Letters 16.7 2014 2/1/2016 9/16/2014 18 16 29877 3
## 2 Ecology Letters 16.7 2014 2/1/2016 10/13/2014 15 12 5997 8
## 3 Ecology Letters 16.7 2014 2/1/2016 10/21/2014 5 4 1667 1
## 4 Ecology Letters 16.7 2014 2/1/2016 8/28/2014 9 8 3482 2
## 5 Ecology Letters 16.7 2014 2/1/2016 8/28/2014 3 3 1329 1
## 6 Ecology Letters 16.7 2014 2/2/2016 7/28/2014 27 23 41906 9
## 7 Ecology Letters 16.7 2014 2/2/2016 8/6/2014 6 6 12223 6
## 8 Ecology Letters 16.7 2014 2/2/2016 6/17/2014 19 18 22020 9
## 9 Ecology Letters 16.7 2014 2/2/2016 6/12/2014 26 23 23003 5
## 10 Ecology Letters 16.7 2014 2/2/2016 7/17/2014 44 42 131788 4
## # … with 1,589 more rows
```
---
# Split a column in several columns
```r
citations %>%
tidyr::separate(pubdate, c('month', 'day', 'year'), sep = '/')
```
```
## # A tibble: 1,599 × 14
## journal impactfactor pubyear Volume Issue Authors colldate month day year nbtweets `Number of user…
## <fct> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl>
## 1 Ecology Letters 16.7 2014 17 12 Morin et … 2/1/2016 9 16 2014 18 16
## 2 Ecology Letters 16.7 2014 17 12 Jucker et… 2/1/2016 10 13 2014 15 12
## 3 Ecology Letters 16.7 2014 17 12 Calcagno … 2/1/2016 10 21 2014 5 4
## 4 Ecology Letters 16.7 2014 17 11 Segre et … 2/1/2016 8 28 2014 9 8
## 5 Ecology Letters 16.7 2014 17 11 Kaufman e… 2/1/2016 8 28 2014 3 3
## 6 Ecology Letters 16.7 2014 17 10 Nasto et … 2/2/2016 7 28 2014 27 23
## 7 Ecology Letters 16.7 2014 17 10 Tschirren… 2/2/2016 8 6 2014 6 6
## 8 Ecology Letters 16.7 2014 17 9 Barnechi … 2/2/2016 6 17 2014 19 18
## 9 Ecology Letters 16.7 2014 17 9 Pinto-San… 2/2/2016 6 12 2014 26 23
## 10 Ecology Letters 16.7 2014 17 9 Clough et… 2/2/2016 7 17 2014 44 42
## # … with 1,589 more rows, and 2 more variables: Twitter reach <dbl>, woscitations <dbl>
```
---
# Transform in Date format...
```r
citations %>%
dplyr::mutate(
pubdate = lubridate::mdy(pubdate),
colldate = lubridate::mdy(colldate))
```
```
## # A tibble: 1,599 × 12
## journal impactfactor pubyear Volume Issue Authors colldate pubdate nbtweets `Number of user… `Twitter reach`
## <fct> <dbl> <dbl> <dbl> <chr> <chr> <date> <date> <dbl> <dbl> <dbl>
## 1 Ecolog… 16.7 2014 17 12 Morin … 2016-02-01 2014-09-16 18 16 29877
## 2 Ecolog… 16.7 2014 17 12 Jucker… 2016-02-01 2014-10-13 15 12 5997
## 3 Ecolog… 16.7 2014 17 12 Calcag… 2016-02-01 2014-10-21 5 4 1667
## 4 Ecolog… 16.7 2014 17 11 Segre … 2016-02-01 2014-08-28 9 8 3482
## 5 Ecolog… 16.7 2014 17 11 Kaufma… 2016-02-01 2014-08-28 3 3 1329
## 6 Ecolog… 16.7 2014 17 10 Nasto … 2016-02-02 2014-07-28 27 23 41906
## 7 Ecolog… 16.7 2014 17 10 Tschir… 2016-02-02 2014-08-06 6 6 12223
## 8 Ecolog… 16.7 2014 17 9 Barnec… 2016-02-02 2014-06-17 19 18 22020
## 9 Ecolog… 16.7 2014 17 9 Pinto-… 2016-02-02 2014-06-12 26 23 23003
## 10 Ecolog… 16.7 2014 17 9 Clough… 2016-02-02 2014-07-17 44 42 131788
## # … with 1,589 more rows, and 1 more variable: woscitations <dbl>
```
---
# ...for easy manipulation of dates
```r
citations %>%
dplyr::mutate(
pubdate = lubridate::mdy(pubdate),
colldate = lubridate::mdy(colldate),
* pubyear2 = lubridate::year(pubdate))
```
```
## # A tibble: 1,599 × 13
## journal impactfactor pubyear Volume Issue Authors colldate pubdate nbtweets `Number of user… `Twitter reach`
## <fct> <dbl> <dbl> <dbl> <chr> <chr> <date> <date> <dbl> <dbl> <dbl>
## 1 Ecolog… 16.7 2014 17 12 Morin … 2016-02-01 2014-09-16 18 16 29877
## 2 Ecolog… 16.7 2014 17 12 Jucker… 2016-02-01 2014-10-13 15 12 5997
## 3 Ecolog… 16.7 2014 17 12 Calcag… 2016-02-01 2014-10-21 5 4 1667
## 4 Ecolog… 16.7 2014 17 11 Segre … 2016-02-01 2014-08-28 9 8 3482
## 5 Ecolog… 16.7 2014 17 11 Kaufma… 2016-02-01 2014-08-28 3 3 1329
## 6 Ecolog… 16.7 2014 17 10 Nasto … 2016-02-02 2014-07-28 27 23 41906
## 7 Ecolog… 16.7 2014 17 10 Tschir… 2016-02-02 2014-08-06 6 6 12223
## 8 Ecolog… 16.7 2014 17 9 Barnec… 2016-02-02 2014-06-17 19 18 22020
## 9 Ecolog… 16.7 2014 17 9 Pinto-… 2016-02-02 2014-06-12 26 23 23003
## 10 Ecolog… 16.7 2014 17 9 Clough… 2016-02-02 2014-07-17 44 42 131788
## # … with 1,589 more rows, and 2 more variables: woscitations <dbl>, pubyear2 <dbl>
```
* Check out `?lubridate::lubridate` for more functions
---
# How to join tables together?
<blockquote class="twitter-tweet" data-lang="fr"><p lang="en" dir="ltr">More <a href="https://twitter.com/hashtag/dplyr?src=hash&amp;ref_src=twsrc%5Etfw">#dplyr</a> 🔧 gifs! It took me a hella long time to wrap my head around the different types of joins when I first started learning them, so here&#39;s a few examples with some excellent mini datasets from <a href="https://twitter.com/hashtag/dplyr?src=hash&amp;ref_src=twsrc%5Etfw">#dplyr</a> designed specifically for this purpose! <a href="https://twitter.com/hashtag/rstats?src=hash&amp;ref_src=twsrc%5Etfw">#rstats</a> <a href="https://twitter.com/hashtag/tidyverse?src=hash&amp;ref_src=twsrc%5Etfw">#tidyverse</a> <a href="https://t.co/G56fWmIZSq">pic.twitter.com/G56fWmIZSq</a></p>&mdash; Nic Crane (@nic_crane) <a href="https://twitter.com/nic_crane/status/1064237554910806016?ref_src=twsrc%5Etfw">18 novembre 2018</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
[![Watch the video](mp4/dplyr_join.mp4)](mp4/dplyr_join.mp4)
---
## <https://www.garrickadenbuie.com/project/tidyexplain/>
<img src="img/left-join.gif" width="70%" style="display: block; margin: auto;" />
---
class: inverse, center, middle
# Easy character manipulation
---
# Select rows corresponding to papers with more than 3 authors
```r
citations %>%
* dplyr::filter(stringr::str_detect(Authors, 'et al'))
```
```
## # A tibble: 1,280 × 12
## journal impactfactor pubyear Volume Issue Authors colldate pubdate nbtweets `Number of user… `Twitter reach`
## <fct> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
## 1 Ecology … 16.7 2014 17 12 Morin et … 2/1/2016 9/16/2… 18 16 29877
## 2 Ecology … 16.7 2014 17 12 Jucker et… 2/1/2016 10/13/… 15 12 5997
## 3 Ecology … 16.7 2014 17 12 Calcagno … 2/1/2016 10/21/… 5 4 1667
## 4 Ecology … 16.7 2014 17 11 Segre et … 2/1/2016 8/28/2… 9 8 3482
## 5 Ecology … 16.7 2014 17 11 Kaufman e… 2/1/2016 8/28/2… 3 3 1329
## 6 Ecology … 16.7 2014 17 10 Nasto et … 2/2/2016 7/28/2… 27 23 41906
## 7 Ecology … 16.7 2014 17 10 Tschirren… 2/2/2016 8/6/20… 6 6 12223
## 8 Ecology … 16.7 2014 17 9 Barnechi … 2/2/2016 6/17/2… 19 18 22020
## 9 Ecology … 16.7 2014 17 9 Pinto-San… 2/2/2016 6/12/2… 26 23 23003
## 10 Ecology … 16.7 2014 17 9 Clough et… 2/2/2016 7/17/2… 44 42 131788
## # … with 1,270 more rows, and 1 more variable: woscitations <dbl>
```
---
# Get column with rows corresponding to papers with more than 3 authors
```r
citations %>%
* dplyr::filter(stringr::str_detect(Authors, 'et al')) %>%
* dplyr::select(Authors)
```
```
## # A tibble: 1,280 × 1
## Authors
## <chr>
## 1 Morin et al
## 2 Jucker et al
## 3 Calcagno et al
## 4 Segre et al
## 5 Kaufman et al
## 6 Nasto et al
## 7 Tschirren et al
## 8 Barnechi et al
## 9 Pinto-Sanchez et al
## 10 Clough et al
## # … with 1,270 more rows
```
---
# Select rows corresponding to papers with less than 3 authors
```r
citations %>%
* dplyr::filter(!stringr::str_detect(Authors, 'et al'))
```
```
## # A tibble: 319 × 12
## journal impactfactor pubyear Volume Issue Authors colldate pubdate nbtweets `Number of user… `Twitter reach`
## <fct> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
## 1 Ecology … 16.7 2014 17 6 Neutle an… 2/15/20… 3/17/2… 8 7 10823
## 2 Ecology … 16.7 2014 17 5 Kellner a… 2/15/20… 2/20/2… 18 18 60309
## 3 Ecology … 16.7 2014 17 4 Griffin a… 2/15/20… 1/16/2… 4 4 9404
## 4 Ecology … 16.7 2014 17 3 Gremer an… 2/15/20… 1/17/2… 4 4 17927
## 5 Ecology … 16.7 2014 17 2 Cavieres 2/15/20… 10/17/… 16 15 18472
## 6 Ecology … 16.7 2014 17 2 Haegman a… 2/15/20… 12/5/2… 9 9 13211
## 7 Ecology … 16.7 2013 16 12 Kearney 2/15/20… 10/1/2… 13 13 37990
## 8 Ecology … 16.7 2013 16 9 Locey and… 2/15/20… 7/15/2… 28 24 51145
## 9 Ecology … 16.7 2013 16 8 Quintero … 2/15/20… 6/26/2… 120 120 686154
## 10 Ecology … 16.7 2013 16 3 Lesser an… 2/15/20… 12/22/… 9 9 12054
## # … with 309 more rows, and 1 more variable: woscitations <dbl>
```
---
# Get column with rows corresponding to papers with less than 3 authors
```r
citations %>%
* dplyr::filter(!stringr::str_detect(Authors, 'et al')) %>%
* dplyr::select(Authors)
```
```
## # A tibble: 319 × 1
## Authors
## <chr>
## 1 Neutle and Thorne
## 2 Kellner and Asner
## 3 Griffin and Willi
## 4 Gremer and Venable
## 5 Cavieres
## 6 Haegman and Loreau
## 7 Kearney
## 8 Locey and White
## 9 Quintero and Weins
## 10 Lesser and Jackson
## # … with 309 more rows
```
---
# Get column with rows corresponding to papers with less than 3 authors
```r
citations %>%
dplyr::filter(!stringr::str_detect(Authors, 'et al')) %>%
* dplyr::pull(Authors) %>%
head(10)
```
```
## [1] "Neutle and Thorne" "Kellner and Asner" "Griffin and Willi" "Gremer and Venable" "Cavieres"
## [6] "Haegman and Loreau" "Kearney" "Locey and White" "Quintero and Weins" "Lesser and Jackson"
```
---
# Select rows corresponding to papers with less than 3 authors in journal with IF < 5
```r
citations %>%
* dplyr::filter(!stringr::str_detect(Authors, 'et al'), impactfactor < 5)
```
```
## # A tibble: 77 × 12
## journal impactfactor pubyear Volume Issue Authors colldate pubdate nbtweets `Number of user… `Twitter reach`
## <fct> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
## 1 Molecular… 4.9 2014 14 6 Gautier 2/27/20… 5/14/2… 2 2 1015
## 2 Molecular… 4.9 2014 14 5 Gambel … 2/27/20… 3/7/20… 7 5 5302
## 3 Molecular… 4.9 2014 14 4 Kekkone… 2/27/20… 3/10/2… 4 4 2284
## 4 Molecular… 4.9 2014 14 3 Bhattac… 2/27/20… 12/8/2… 0 0 0
## 5 Molecular… 4.9 2014 14 1 Christi… 2/28/20… 10/25/… 0 0 0
## 6 Molecular… 4.9 2013 13 4 Villard… 2/28/20… 5/2/20… 0 0 0
## 7 Molecular… 4.9 2013 13 4 Wang 2/28/20… 4/25/2… 0 0 0
## 8 Molecular… 4.9 2012 12 1 Joly 2/28/20… 9/7/20… 3 3 1861
## 9 Animal Co… 3.21 2014 17 6 Plavsic 2/9/2016 4/17/2… 9 9 12822
## 10 Animal Co… 3.21 2014 17 Suppl… Knox an… 2/11/20… 11/13/… 1 1 206
## # … with 67 more rows, and 1 more variable: woscitations <dbl>
```
---
# Convert words to lowercase
```r
citations %>%
* dplyr::mutate(authors_lowercase = stringr::str_to_lower(Authors)) %>%
dplyr::select(authors_lowercase)
```
```
## # A tibble: 1,599 × 1
## authors_lowercase
## <chr>
## 1 morin et al
## 2 jucker et al
## 3 calcagno et al
## 4 segre et al
## 5 kaufman et al
## 6 nasto et al
## 7 tschirren et al
## 8 barnechi et al
## 9 pinto-sanchez et al
## 10 clough et al
## # … with 1,589 more rows
```
---
# Remove all spaces in journal names
```r
citations %>%
* dplyr::mutate(journal = stringr::str_remove_all(journal, " ")) %>%
dplyr::select(journal) %>%
unique() %>%
head(5)
```
```
## # A tibble: 5 × 1
## journal
## <chr>
## 1 EcologyLetters
## 2 GlobalChangeBiology
## 3 GlobalEcologyandBiogeography
## 4 MolecularEcologyResources
## 5 DiversityandDistributions
```
---
# Explore 📦 stringr and regular expressions
* Check out the [vignette on stringr](https://cran.r-project.org/web/packages/stringr/vignettes/stringr.html) for more examples on character manipulation and pattern matching functions.
* Check out the [vignette on regular expressions](https://stringr.tidyverse.org/articles/regular-expressions.html) which are a concise and flexible tool for describing patterns in strings.
---
class: inverse, center, middle
# Basic exploratory data analysis
---
# Count
```r
citations %>% dplyr::count(journal, sort = TRUE)
```
```
## # A tibble: 20 × 2
## journal n
## <fct> <int>
## 1 New Phytologist 144
## 2 Ecology 108
## 3 Evolution 108
## 4 Global Change Biology 108
## 5 Global Ecology and Biogeography 108
## 6 Journal of Biogeography 108
## 7 Ecology Letters 106
## 8 Diversity and Distributions 105
## 9 Animal Conservation 102
## 10 Methods in Ecology and Evolution 90
## 11 Evolutionary Applications 74
## 12 Functional Ecology 54
## 13 Journal of Animal Ecology 54
## 14 Journal of Applied Ecology 54
## 15 Limnology and Oceanography 54
## 16 Molecular Ecology Resources 54
## 17 Conservation Letters 53
## 18 Ecological Applications 48
## 19 Fish and Fisheries 36
## 20 Mammal Review 31
```
---
# Count
```r
citations %>%
dplyr::count(journal, pubyear) %>%
head()
```
```
## # A tibble: 6 × 3
## journal pubyear n
## <fct> <dbl> <int>
## 1 Animal Conservation 2012 18
## 2 Animal Conservation 2013 18
## 3 Animal Conservation 2014 66
## 4 Conservation Letters 2012 17
## 5 Conservation Letters 2013 18
## 6 Conservation Letters 2014 18
```
---
# Count sum of tweets per journal
```r
citations %>%
dplyr::count(journal, wt = nbtweets, sort = TRUE)
```
```
## # A tibble: 20 × 2
## journal n
## <fct> <dbl>
## 1 Ecology Letters 1538
## 2 Animal Conservation 1268
## 3 Journal of Applied Ecology 1012
## 4 Methods in Ecology and Evolution 699
## 5 Global Change Biology 613
## 6 Conservation Letters 542
## 7 New Phytologist 509
## 8 Global Ecology and Biogeography 379
## 9 Ecology 335
## 10 Evolution 335
## 11 Journal of Animal Ecology 323
## 12 Fish and Fisheries 261
## 13 Evolutionary Applications 238
## 14 Journal of Biogeography 209
## 15 Diversity and Distributions 200
## 16 Mammal Review 166
## 17 Functional Ecology 155
## 18 Molecular Ecology Resources 139
## 19 Ecological Applications 125
## 20 Limnology and Oceanography 0
```
---
# Group by variable to calculate stats
```r
citations %>%
* dplyr::group_by(journal) %>%
* dplyr::summarise(avg_tweets = mean(nbtweets)) %>%
head(10)
```
```
## # A tibble: 10 × 2
## journal avg_tweets
## <fct> <dbl>
## 1 Animal Conservation 12.4
## 2 Conservation Letters 10.2
## 3 Diversity and Distributions 1.90
## 4 Ecological Applications 2.60
## 5 Ecology 3.10
## 6 Ecology Letters 14.5
## 7 Evolution 3.10
## 8 Evolutionary Applications 3.22
## 9 Fish and Fisheries 7.25
## 10 Functional Ecology 2.87
```
---
# Order stuff
```r
citations %>%
dplyr::group_by(journal) %>%
dplyr::summarise(avg_tweets = mean(nbtweets)) %>%
* dplyr::arrange(dplyr::desc(avg_tweets)) %>% # decreasing order (wo desc for increasing)
head(10)
```
```
## # A tibble: 10 × 2
## journal avg_tweets
## <fct> <dbl>
## 1 Journal of Applied Ecology 18.7
## 2 Ecology Letters 14.5
## 3 Animal Conservation 12.4
## 4 Conservation Letters 10.2
## 5 Methods in Ecology and Evolution 7.77
## 6 Fish and Fisheries 7.25
## 7 Journal of Animal Ecology 5.98
## 8 Global Change Biology 5.68
## 9 Mammal Review 5.35
## 10 New Phytologist 3.53
```
---
# What if we want to work on several columns?
<img src="img/dplyr_across.png" width="85%" style="display: block; margin: auto;" />
---
# Compute mean across all numeric columns for each journal
```r
citations %>%
* dplyr::group_by(journal) %>%
* dplyr::summarize(dplyr::across(where(is.numeric), mean)) %>%
head()
```
```
## # A tibble: 6 × 8
## journal impactfactor pubyear Volume nbtweets `Number of users` `Twitter reach` woscitations
## <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Animal Conservation 3.21 2013. 16.5 12.4 9.71 28345. 4.43
## 2 Conservation Letters 6.4 2013. 6.02 10.2 8.85 23234. 9.30
## 3 Diversity and Distributions 5.4 2013 19 1.90 1.77 2350. 10.2
## 4 Ecological Applications 5.06 2013 23 2.60 2.5 5727. 10.7
## 5 Ecology 6.16 2013 94 3.10 2.87 6176. 11.1
## 6 Ecology Letters 16.7 2013. 16.0 14.5 14.0 44748. 20.6
```
---
## <https://github.com/courtiol/Rguides>
<img src="img/dplyr_guide_for_one_table_part2.png" width="85%" style="display: block; margin: auto;" />
---
# Tidying tibbles
<img src="img/original-dfs-tidy.png" width="70%" style="display: block; margin: auto;" />
---
## Going from **long** to **wide** format and vice-versa
<img src="img/tidyr-longer-wider.gif" width="70%" style="display: block; margin: auto;" />
---
class: inverse, center, middle
# Visualize
---
# Visualization with ggplot2
* The package ggplot2 implements a **g**rammar of **g**raphics
* Operates on data.frames or tibbles, not vectors like base R
* Explicitly differentiates between the data and its representation
<img src="img/ggplot2_logo.jpg" width="30%" style="display: block; margin: auto;" />
---
# The ggplot2 grammar
Grammar element | What it is
:---------------- | :-----------------------------
**Data** | The data frame being plotted
**Geometrics** | The geometric shape that will represent the data
| (e.g., point, boxplot, histogram)
**Aesthetics** | The aesthetics of the geometric object
| (e.g., color, size, shape)
<img src="img/ggplot2_logo.jpg" width="30%" style="display: block; margin: auto;" />
---
# Scatterplots