-
Notifications
You must be signed in to change notification settings - Fork 10
/
slide_tidyverse.Rmd
738 lines (533 loc) · 15.7 KB
/
slide_tidyverse.Rmd
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
---
title: "Tidy work in Tidyverse"
subtitle: "R Foundation for Life Scientists"
author: "Marcin Kierczak"
keywords: r, rstats, r programming, markdown, tidyverse
output:
xaringan::moon_reader:
encoding: 'UTF-8'
self_contained: false
chakra: 'assets/remark-latest.min.js'
css: 'assets/slide.css'
lib_dir: libs
include: NULL
nature:
ratio: '4:3'
highlightLanguage: r
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
slideNumberFormat: "%current%/%total%"
---
exclude: true
count: false
```{r,echo=FALSE,child="assets/header-slide.Rmd"}
```
<!-- ----------------- Only edit title & author above this ----------------- -->
```{r setup,include=FALSE}
knitr::opts_chunk$set(dev="png",results="hold",fig.show="hold",fig.align="left",echo=TRUE,warning=FALSE,message=FALSE)
options(crayon.enabled = FALSE)
# report related
library(knitr)
library(markdown)
library(rmarkdown)
```
```{r,echo=FALSE,message=FALSE,warning=FALSE}
# load the packages you need
library(tidyverse)
library(ggplot2) # static graphics
library(kableExtra)
library(magrittr)
library(emo)
```
---
name: setup_livecode
# Livecode Setup
By typing:
`http://livecode.kierczak.net:7777`
in your browser, you can access the livecode server.
---
name: learning_outcomes
# Learning Outcomes
<br>
Upon completing this module, you will:
* know what `tidyverse` is and a bit about its history
* be aware of useful packages within `tidyverse`
* be able to use basic pipes (including native R pipe)
* know whether the data you are working with are tidy
* will be able to do basic tidying of your data
---
name: tidyverse_overview
# Tidyverse -- What is it all About?
* [tidyverse](http://www.tidyverse.org) is a collection of `r emo::ji('package')` `r emo::ji('package')`
* created by [Hadley Wickham](http://hadley.nz)
* has become a *de facto* standard in data analyses
* a philosophy of programming or a **programming paradigm**: everything is about the `r emo::ji('water_wave')` flow of `r emo::ji('broom')` tidy data
.center[
<img src="data/slide_tidyverse/hex-tidyverse.png", style="height:200px;">
<img src="data/slide_tidyverse/Hadley-wickham2016-02-04.jpeg", style="height:200px;">
<img src="data/slide_tidyverse/RforDataScience.jpeg", style="height:200px;">
]
.vsmall[sources of images: www.tidyverse.org, Wikipedia, www.tidyverse.org]
---
name: tidyverse_curse
# ?(Tidyverse OR !Tidyverse)
> `r emo::ji('skull_and_crossbones')` There are still some people out there talking about the tidyverse curse though... `r emo::ji('skull_and_crossbones')`<br>
--
> Navigating the balance between base R and the tidyverse is a challenge to learn.<br>[-Robert A. Muenchen](http://r4stats.com/articles/why-r-is-hard-to-learn/)
--
.center[<img src="data/slide_tidyverse/tidyverse-flow.png", style="height:400px;">]
.vsmall[source: http://www.storybench.org/getting-started-with-tidyverse-in-r/]
---
name: intro_to_pipes
# Pipes or Let my Data Flow `r emo::ji('water_wave')`
.pull-left-50[
.center[<img src="data/slide_tidyverse/pipe_magritte.jpg", style="width:300px;">]
.vsmall[Rene Magritt, *La trahison des images*, [Wikimedia Commons](https://en.wikipedia.org/wiki/The_Treachery_of_Images#/media/File:MagrittePipe.jpg)]
.center[<img src="data/slide_tidyverse/magrittr.png", style="width:150px;">]
]
--
.pull-right-50[
* Let the data flow.
* *Ceci n'est pas une pipe* -- `magrittr`
* The `%>%` pipe:
+ `x %>% f` $\equiv$ `f(x)`
+ `x %>% f(y)` $\equiv$ `f(x, y)`
+ `x %>% f %>% g %>% h` $\equiv$ `h(g(f(x)))`
]
--
.pull-right-50[
instead of writing this:
```{r magrittr0, eval=F}
data <- iris
data <- head(data, n=3)
```
]
--
.pull-right-50[
write this:
```{r magrittr1}
iris %>% head(n=3)
```
]
---
name: native_r_pipe
# Native R Pipe
From R 4.1.0, we have a native pipe operator `|>` that is a bit faster than the `magrittr` pipe `%>%`.
It, however, differs from the `magrittr` pipe in some aspects, e.g., it does not allow for the use of the dot `.` as a placeholder (it has a simple `_` placeholder though).
```{r native_pipe1}
c(1:5) |> mean()
```
```{r native_pipe2}
c(1:5) %>% mean()
```
---
name: tibble_intro
# Tibbles
.pull-left-50[
.center[<img src="data/slide_tidyverse/tibble_tweet.jpg">]
]
.pull-right-50[
* `tibble` is one of the unifying features of tidyverse,
* it is a *better* `data.frame` realization,
* objects `data.frame` can be coerced to `tibble` using `as_tibble()`
]
---
name: convert_to_tibble
# Convert `data.frame` to `tibble`
```{r}
as_tibble(iris)
```
---
name: tibble_from_scratch
# Tibbles from scratch with `tibble()`
```{r tibble_from_scratch, eval=FALSE}
tibble(
x = 1, # recycling
y = runif(4),
z = x + y^2,
outcome = rnorm(4)
)
```
--
```{r tibble_from_scratch_eval, echo = F, eval=TRUE}
tibble(
x = 1, # recycling
y = runif(4),
z = x + y^2,
outcome = rnorm(4)
)
```
---
name: more_on_tibbles
# More on Tibbles
* When you print a `tibble`:
+ all columns that fit the screen are shown,
+ first 10 rows are shown,
+ data type for each column is shown.
```{r tibble_printing}
as_tibble(cars)
```
---
name: tibble_printing_options
# Tibble Printing Options
* `my_tibble %>% print(n = 50, width = Inf)`,
* `options(tibble.print_min = 15, tibble.print_max = 25)`,
* `options(dplyr.print_min = Inf)`,
* `options(tibble.width = Inf)`
---
name: subsetting_tibbles
# Subsetting Tibbles
```{r tibble_subs}
vehicles <- as_tibble(cars[1:5,])
vehicles %>% print(n = 5)
```
--
We can subset tibbles in a number of ways:
```{r tibble_subs1}
vehicles[['speed']] # try also vehicles['speed']
vehicles[[1]]
vehicles$speed
```
--
> **Note!** Not all old R functions work with tibbles, than you have to use `as.data.frame(my_tibble)`.
---
name: tibbles_partial_matching
# Tibbles are Stricter than `data.frames`
```{r tibble_strict, warning=T, echo=F}
cars <- cars[1:5,]
```
```{r tibble_strict1, warning=T}
cars$spe # partial matching
```
```{r tibble_strict2, warning=T}
vehicles$spe # no partial matching
```
```{r tibble_strict3, warning=T}
cars$gear
```
```{r tibble_strict4, warning=T}
vehicles$gear
```
---
name: loading_data
# Loading Data
In `tidyverse` you import data using `readr` package that provides a number of useful data import functions:
* `read_delim()` a generic function for reading *-delimited files. There are a number of convenience wrappers:
+ `read_csv()` used to read comma-delimited files,
+ `read_csv2()` reads semicolon-delimited files,
`read_tsv()` that reads tab-delimited files.
* `read_fwf` for reading fixed-width files with its wrappers:
+ fwf_widths() for width-based reading,
+ fwf_positions() for positions-based reading and
+ read_table() for reading white space-delimited fixed-width files.
* `read_log()` for reading Apache-style logs.
--
>The most commonly used `read_csv()` has some familiar arguments like:
* `skip` -- to specify the number of rows to skip (headers),
* `col_names` -- to supply a vector of column names,
* `comment` -- to specify what character designates a comment,
* `na` -- to specify how missing values are represented.
---
name: readr_writing
# Writing to a File
The `readr` package also provides functions useful for writing tibbled data into a file:
* `write_csv()`
* `write_tsv()`
* `write_excel_csv()`
They **always** save:
* text in UTF-8,
* dates in ISO8601
But saving in csv (or tsv) does mean you loose information about the type of data in particular columns. You can avoid this by using:
* `write_rds()` and `read_rds()` to read/write objects in R binary rds format,
* use `write_feather()` and `read_feather()` from package `feather` to read/write objects in a fast binary format that other programming languages can access.
---
name: basic_data_transformations
# Basic Data Transformations with `dplyr`
Let us create a tibble:
```{r}
bijou <- as_tibble(diamonds) %>% head()
bijou[1:5, ]
```
.center[ <img src="data/slide_tidyverse/diamonds.png", style="height:200px"> ]
---
name: filter
# Picking Observations using `filter()`
```{r}
bijou %>% filter(cut == 'Ideal' | cut == 'Premium', carat >= 0.23) %>% head(n = 4)
```
--
>`r emo::ji('boat')` Be careful with floating point comparisons! <br>
`r emo::ji('pirate')` Also, rows with comparison resulting in `NA` are skipped by default!
```{r, echo=T, eval=F}
bijou %>% filter(near(0.23, carat) | is.na(carat)) %>% head(n = 4)
```
---
name: arrange
# Rearranging Observations using `arrange()`
```{r, echo=T, eval=FALSE}
bijou %>% arrange(cut, carat, desc(price))
```
--
```{r, echo=FALSE, eval=TRUE}
bijou %>% arrange(cut, carat, desc(price))
```
--
>The `NA`s always end up at the end of the rearranged `tibble`!
---
name: select
# Selecting Variables with `select()`
Simple `select` with a range:
```{r}
bijou %>% select(color, clarity, x:z) %>% head(n = 4)
```
--
Exclusive `select`:
```{r}
bijou %>% select(-(x:z)) %>% head(n = 4)
```
---
name: rename
# Renaming Variables
>`rename` is a variant of `select`, here used with `everything()` to move `x` to the beginning and rename it to `var_x`
```{r, eval=FALSE, echo=TRUE}
bijou %>% rename(var_x = x) %>% head(n = 5)
```
--
```{r, eval=T, echo=F}
bijou %>% rename(var_x = x) %>% head(n = 5)
```
---
name: bring_to_front
# Bring columns to front
>use `everything()` to bring some columns to the front:
```{r, echo=TRUE, eval=FALSE}
bijou %>% select(x:z, everything()) %>% head(n = 4)
```
--
```{r, echo=FALSE, eval=TRUE}
bijou %>% select(x:z, everything()) %>% head(n = 4)
```
---
name: mutate
# Create/alter new Variables with `mutate`
```{r, echo=T, eval=F}
bijou %>% mutate(p = x + z, q = p + y) %>%
select(-(depth:price)) %>%
head(n = 5)
```
--
```{r, echo=F, eval=T}
bijou %>% mutate(p = x + z, q = p + y) %>%
select(-(depth:price)) %>%
head(n = 5)
```
---
name: transmute
# Create/alter new Variables with `transmute` `r emo::ji('wizard')`
>Only the transformed variables will be retained.
```{r}
bijou %>% transmute(carat, cut, sum = x + y + z) %>% head(n = 5)
```
---
name: grouped_summaries
# Group and Summarize
```{r}
bijou %>% group_by(cut) %>% summarize(max_price = max(price),
mean_price = mean(price),
min_price = min(price))
```
--
```{r}
bijou %>% group_by(cut, color) %>%
summarize(max_price = max(price),
mean_price = mean(price),
min_price = min(price)) %>% head(n = 4)
```
---
name: other_data_manipulations
# Other data manipulation tips
```{r}
bijou %>% group_by(cut) %>% summarize(count = n())
```
--
When you need to regroup within the same pipe, use `ungroup()`.
---
name: concept_of_tidy_data
# The Concept of Tidy Data
Data are tidy *sensu Wickham* if:
* each and every observation is represented as exactly one row,
* each and every variable is represented by exactly one column,
* thus each data table cell contains only one value.
`r knitr::include_graphics("data/slide_tidyverse/tidy_data.png")`
Usually data are untidy in only one way. However, if you are unlucky, they are really untidy and thus a pain to work with...
---
name: tidy_data
# Tidy Data
<img src="data/slide_tidyverse/tidy_data.png" style="height:100px">
--
.center[**Are these data tidy?**]
.pull-left-70[
```{r tidy_iris1, echo=FALSE}
data("iris")
iris %>% head(n=3) %>% kable("html",escape=F,align="c") %>%
kable_styling(bootstrap_options=c("striped","hover","responsive","condensed"),
position="left",full_width = F)
```
]
--
.pull-right-30[
```{r tidy_iris2, echo=FALSE}
iris2 <- iris %>%
gather(key=variable, value=value, -Species)
iris2 %>%
head(n=3) %>%
kable("html",escape=F,align="c") %>%
kable_styling(bootstrap_options=c("striped","hover","responsive","condensed"),
position="left",full_width = F)
```
]
<br> <hr><br>
--
.pull-left-50[
```{r tidy_iris3, echo=FALSE}
iris3 <-
iris %>%
unite(Sepal.L.W, Sepal.Length, Sepal.Width, sep = "/") %>%
unite(Petal.L.W, Petal.Length, Petal.Width, sep = "/")
iris3 %>%
head(n = 3) %>%
kable("html",escape=F,align="c") %>%
kable_styling(bootstrap_options=c("striped","hover","responsive","condensed"),
position="left",full_width = F)
```
]
--
.pull-right-50[
```{r tidy_iris4, echo=FALSE}
iris4 <- t(iris)
iris4[,1:4] %>%
kable("html",escape=F,align="c") %>%
kable_styling(bootstrap_options=c("striped","hover","responsive","condensed"),
position="left",full_width = F)
```
]
---
name: tidying_data_pivot_longer
# Tidying Data with `tidyr::pivot_longer`
If some of your column names are actually values of a variable, use `pivot_longer` (replaces `gather`):
```{r include=FALSE}
bijou %>%
mutate(`2008` = price) %>%
select(-price) %>%
mutate(`2009` = `2008` + sample(rnorm(100, mean = 0.01 * mean(`2008`)),
size = 1,
replace=T
)
) %>%
select(cut, `2008`, `2009`) -> bijou2
```
```{r bijou2}
bijou2 %>% head(n = 5)
```
```{r}
bijou2 %>%
pivot_longer(c(`2008`, `2009`), names_to = 'year', values_to = 'price') %>%
head(n = 5)
```
---
name: tidying_data_pivot_wider
# Tidying Data with `tidyr::pivot_wider`
If some of your observations are scattered across many rows, use `pivot_wider` (replaces `gather`):
```{r include=FALSE}
bijou %>% head(n = 3) %>% select(cut, price, clarity, x, y, z) %>% gather(x,y,z, key='dimension', value='measurement') -> bijou3
```
```{r bijou3}
bijou3
```
```{r}
bijou3 %>%
pivot_wider(names_from=dimension, values_from=measurement) %>%
head(n = 4)
```
---
name: tidying_data_separate
# Tidying Data with `separate`
If some of your columns contain more than one value, use `separate`:
```{r include=FALSE}
bijou %>% head(n = 5) %>% select(cut, price, clarity, x, y, z) %>% unite(dim, x, y, z, sep='/') -> bijou4
```
```{r bijou4}
bijou4
```
```{r}
bijou4 %>%
separate(dim, into = c("x", "y", "z"), sep = "/", convert = T)
```
---
name: tidying_data_unite
# Tidying Data with `unite`
If some of your columns contain more than one value, use `separate`:
```{r include=FALSE}
bijou %>% head(n = 5) %>% select(cut, price, clarity, x, y, z) %>% separate(clarity, into = c('clarity_prefix', 'clarity_suffix'), sep = 2) -> bijou5
```
```{r bijou5}
bijou5
```
```{r}
bijou5 %>% unite(clarity, clarity_prefix, clarity_suffix, sep='')
```
---
name: missing_complete
# Completing Missing Values Using `complete`
```{r eval=FALSE, include=FALSE}
bijou %>%
head(n = 10) %>%
select(cut, clarity, price) %>%
mutate(cut, cut2=replace(cut, sample(1:10, 4, F), NA)) -> missing_stones
```
```{r}
bijou %>% head(n = 10) %>%
select(cut, clarity, price) %>%
mutate(continent = sample(c('AusOce', 'Eur'),
size = 6,
replace = T)) -> missing_stones
```
```{r}
missing_stones %>% complete(cut, continent)
```
---
name: joins
# Joining Data with `_join`
```{r echo=FALSE, eval=TRUE}
# create two tibbles that share key column and can illustrate joins
tibble1 <- tibble(key = c(1, 2, 3, 4, 5), value1 = c('a', 'b', 'c', 'd', 'e'))
tibble2 <- tibble(key = c(1, 2, 3, 6, 7), value2 = c('A', 'B', 'C', 'F', 'G'))
```
.pull-left-50[
```{r echo=FALSE}
print(tibble1)
```
]
.pull-right-50[
```{r echo=FALSE}
print(tibble2)
```
]
**Example:**
```{r}
inner_join(tibble1, tibble2, by = 'key')
```
`[inner, left, right, full]_join` are available. Try these!
---
name: more_tidyverse
# Some Other Friends
* `stringr` for string manipulation and regular expressions,
* `forcats` for working with factors,
* `lubridate` for working with dates.
---
name: end-slide
class: end-slide
# Thank you. Questions? [More?](https://nbisweden.github.io/raukr-2024/)
```{r,echo=FALSE,child="assets/footer-slide.Rmd"}
```