-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-dplyr_cont.html
818 lines (671 loc) · 22.6 KB
/
10-dplyr_cont.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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>dplyr (continuación)</title>
<meta charset="utf-8" />
<meta name="author" content="Alberto Torres" />
<meta name="date" content="2021-01-07" />
<link href="libs/remark-css-0.0.1/default.css" rel="stylesheet" />
<link rel="stylesheet" href="custom.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: center, middle, inverse, title-slide
# dplyr (continuación)
## Entornos de Análisis de Datos: R
### Alberto Torres
### 2021-01-07
---
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
Macros: {
Xcal: "{\\mathcal{X}}",
Xbf: "{\\mathbf{X}}",
Qbf: "{\\mathbf{Q}}",
Zbf: "{\\mathbf{Z}}",
Vbf: "{\\mathbf{V}}",
Hbf: "{\\mathbf{H}}",
Rbb: "{\\mathbb{R}}"
},
extensions: ["AMSmath.js","AMSsymbols.js"]
}
});
</script>
## Operaciones agrupadas
- La función `group_by()` convierte un data frame en otro agrupado por una o más variables
- En los data frames agrupados todas las operaciones anteriores se realizan "por grupo"
- `ungroup()` elimina la agrupación.
---
## Slice con group_by
- Los indices son relativos al grupo.
```r
mpg %>%
group_by(cyl) %>%
slice(1:2)
## # A tibble: 8 x 11
## # Groups: cyl [4]
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
## 3 volkswagen jetta 2.5 2008 5 auto(s6) f 21 29 r compact
## 4 volkswagen jetta 2.5 2008 5 manual(m5) f 21 29 r compact
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compact
## 7 audi a6 quattro 4.2 2008 8 auto(s6) 4 16 23 p midsize
## 8 chevrolet c1500 suburban 2wd 5.3 2008 8 auto(l4) r 14 20 r suv
```
---
## Select con group_by
- `select()` mantiene siempre las variables agrupadas, aunque no se indique explicitamente.
```r
data <- mpg %>%
group_by(cyl) %>%
select(cty)
## Adding missing grouping variables: `cyl`
glimpse(data)
## Rows: 234
## Columns: 2
## Groups: cyl [4]
## $ cyl <int> 4, 4, 4, 4, 6, 6, 6, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 8, 8, 8, 6, 8, 8, 8...
## $ cty <int> 18, 21, 20, 21, 16, 18, 18, 18, 16, 20, 19, 15, 17, 17, 15, 15, 17, 16, 14, 11, 14, 13, 12, 16, 15, 16, 15, 15, 14, 11, 11, 14, 19, 22, 18, 18, 17, 18, 17, 16, 16, 17, 17, 11, 15, 1...
```
---
## arrange con group_by
- `arrange()` ignora la agrupación, a no ser que el parámetro `.by_group` sea `TRUE`
.pull-left[
```r
mpg %>%
select(year, cty) %>%
group_by(year) %>%
arrange(cty)
## # A tibble: 234 x 2
## # Groups: year [2]
## year cty
## <int> <int>
## 1 2008 9
## 2 2008 9
## 3 2008 9
## 4 2008 9
## 5 2008 9
## 6 2008 11
## 7 2008 11
## 8 1999 11
## 9 2008 11
## 10 1999 11
## # ... with 224 more rows
```
]
.pull-right[
```r
mpg %>%
select(year, cty) %>%
group_by(year) %>%
arrange(cty, .by_group = TRUE)
## # A tibble: 234 x 2
## # Groups: year [2]
## year cty
## <int> <int>
## 1 1999 11
## 2 1999 11
## 3 1999 11
## 4 1999 11
## 5 1999 11
## 6 1999 11
## 7 1999 11
## 8 1999 11
## 9 1999 11
## 10 1999 11
## # ... with 224 more rows
```
]
---
## mutate con group_by
Un `mutate()` sobre un data frame agrupado devuelve siempre otro data frame con el mismo número de filas que el original.
.pull-left[
```r
mpg %>%
# sin group_by
mutate(avg_cty = mean(cty)) %>%
# ungroup no necesario
select(cyl, cty, avg_cty)
## # A tibble: 234 x 3
## cyl cty avg_cty
## <int> <int> <dbl>
## 1 4 18 16.9
## 2 4 21 16.9
## 3 4 20 16.9
## 4 4 21 16.9
## 5 6 16 16.9
## 6 6 18 16.9
## 7 6 18 16.9
## 8 4 18 16.9
## 9 4 16 16.9
## 10 4 20 16.9
## # ... with 224 more rows
```
]
.pull-right[
```r
mpg %>%
group_by(cyl) %>%
mutate(avg_cty = mean(cty)) %>%
ungroup() %>%
select(cyl, cty, avg_cty)
## # A tibble: 234 x 3
## cyl cty avg_cty
## <int> <int> <dbl>
## 1 4 18 21.0
## 2 4 21 21.0
## 3 4 20 21.0
## 4 4 21 21.0
## 5 6 16 16.2
## 6 6 18 16.2
## 7 6 18 16.2
## 8 4 18 21.0
## 9 4 16 21.0
## 10 4 20 21.0
## # ... with 224 more rows
```
]
---
## Metodología split-apply-combine
.center[
![:scale 60%](img/split-apply-combine.svg)
.footnotesize[[Jake VanderPlas. Group-by From Scratch](https://jakevdp.github.io/blog/2017/03/22/group-by-from-scratch/)]
]
---
## group_by + summarize
- La metodología anterior se implementa con `group_by + summarize`
```r
mpg %>%
group_by(cyl) %>%
summarize(avg_cty = mean(cty))
## # A tibble: 4 x 2
## cyl avg_cty
## <int> <dbl>
## 1 4 21.0
## 2 5 20.5
## 3 6 16.2
## 4 8 12.6
```
- Devuelve un dataframe con tantas filas como grupos (valores distintos de la/s variable/s usadas para agrupar).
---
## Agrupar por múltiples columnas
- Podemos agrupar por múltiples columnas
```r
mpg %>%
group_by(drv, year) %>%
summarize(avg_hwy = mean(hwy))
## # A tibble: 6 x 3
*## # Groups: drv [3]
## drv year avg_hwy
## <chr> <int> <dbl>
## 1 4 1999 18.8
## 2 4 2008 19.5
## 3 f 1999 27.9
## 4 f 2008 28.4
## 5 r 1999 20.6
## 6 r 2008 21.3
```
- `summarize` elimina un nivel de la agrupación (empezando por la derecha), por lo que hay que tener cuidado si realizamos operaciones posteriores sobre el resultado
---
## Número de grupos
- El número de grupos es el número de combinaciones posibles de los valores distintos
```r
mpg %>%
summarize(n_year = n_distinct(year),
n_trans = n_distinct(drv),
n_comb = n_distinct(year, drv))
## # A tibble: 1 x 3
## n_year n_trans n_comb
## <int> <int> <int>
## 1 2 3 6
```
---
## joins
- La librería `dplyr` implementa funciones para unir data frames:
+ `inner_join(x,y)`: Devuelve las filas que crucen tant en x como en y.
+ `left_join(x,y)`: Devuelve todas, las filas en x y las que crucen en y (completa con NA)
+ `right_join(x,y)`: Devuelve todas las filas en y y las que crucen en x (completa con NA).
+ `full_join(x,y)`: Devuelve todas las filas de x e y (completa con NA).
+ `semi_join(x,y)`: Devuelve solo las filas de x que crucen con y (pero no y).
+ `anti_join(x,y)`: Devuelve solo las filas de x que NO crucen con y.
- Diagrama de Venn. [R for Data Science. Relational data](https://r4ds.had.co.nz/relational-data.html)
.center[
![:scale 70%](img/join-venn.png)
]
---
## Equivalencia con SQL
| dplyr | SQL |
|-----------------------------|------------------------------------------------|
|`inner_join(x, y, by = "z")` | `SELECT * FROM x INNER JOIN y USING (z)` |
|`left_join(x, y, by = "z")` | `SELECT * FROM x LEFT OUTER JOIN y USING (z)` |
|`right_join(x, y, by = "z")` | `SELECT * FROM x RIGHT OUTER JOIN y USING (z)` |
|`full_join(x, y, by = "z")` | `SELECT * FROM x FULL OUTER JOIN y USING (z)` |
.center[
Fuente: [R for Data Science. Relational data](https://r4ds.had.co.nz/relational-data.html)
]
---
## Ejemplo (I)
- **t4a**: Número de casos de tuberculosis en Afganistán, Brasil y China durante los años 1999 y 2000
- **t4b**: Población de Afganistán, Brasil y China durante los años 1999 y 2000
.col-left[
```r
t4a
## # A tibble: 6 x 3
## country year cases
## <chr> <chr> <int>
## 1 Afghanistan 1999 745
## 2 Brazil 1999 37737
## 3 China 1999 212258
## 4 Afghanistan 2000 2666
## 5 Brazil 2000 80488
## 6 China 2000 213766
t4b
## # A tibble: 6 x 3
## country YEAR population
## <chr> <chr> <int>
## 1 Afghanistan 1999 19987071
## 2 Brazil 1999 172006362
## 3 China 1999 1272915272
## 4 Afghanistan 2000 20595360
## 5 Brazil 2000 174504898
## 6 China 2000 1280428583
```
]
.col-right[
```r
join <- inner_join(t4a, t4b,
by=c("year" = "YEAR", "country"))
join
## # A tibble: 6 x 4
## country year cases population
## <chr> <chr> <int> <int>
## 1 Afghanistan 1999 745 19987071
## 2 Brazil 1999 37737 172006362
## 3 China 1999 212258 1272915272
## 4 Afghanistan 2000 2666 20595360
## 5 Brazil 2000 80488 174504898
## 6 China 2000 213766 1280428583
```
]
---
## Ejemplo (II)
```r
x <- tibble(x1=c("A","B","C"),x2=1:3)
x
## # A tibble: 3 x 2
## x1 x2
## <chr> <int>
## 1 A 1
## 2 B 2
## 3 C 3
y <- tibble(x1=c("B","C","D"),x3=2:4)
y
## # A tibble: 3 x 2
## x1 x3
## <chr> <int>
## 1 B 2
## 2 C 3
## 3 D 4
left_join(x,y,by=c("x1"))
## # A tibble: 3 x 3
## x1 x2 x3
## <chr> <int> <int>
## 1 A 1 NA
## 2 B 2 2
## 3 C 3 3
```
---
## Ejemplo (III)
```r
right_join(x,y,by=c("x1"))
## # A tibble: 3 x 3
## x1 x2 x3
## <chr> <int> <int>
## 1 B 2 2
## 2 C 3 3
## 3 D NA 4
full_join(x,y,by=c("x1"))
## # A tibble: 4 x 3
## x1 x2 x3
## <chr> <int> <int>
## 1 A 1 NA
## 2 B 2 2
## 3 C 3 3
## 4 D NA 4
semi_join(x,y,by=c("x1"))
## # A tibble: 2 x 2
## x1 x2
## <chr> <int>
## 1 B 2
## 2 C 3
anti_join(x,y,by=c("x1"))
## # A tibble: 1 x 2
## x1 x2
## <chr> <int>
## 1 A 1
```
---
## Operaciones sobre múltiples columnas
- Una misma operación sobre múltiples columnas
```r
summarize(mpg,
avg_hwy = mean(hwy),
avg_cty = mean(cty),
avg_displ = mean(displ))
## # A tibble: 1 x 3
## avg_hwy avg_cty avg_displ
## <dbl> <dbl> <dbl>
## 1 23.4 16.9 3.47
```
- Se puede reescribir de forma más compacta usando la función `across` (dplyr >= 1.0)
```r
summarize(mpg, across(c(hwy, cty, displ), mean))
## # A tibble: 1 x 3
## hwy cty displ
## <dbl> <dbl> <dbl>
## 1 23.4 16.9 3.47
```
- Más información: [Column-wise operations](https://dplyr.tidyverse.org/dev/articles/colwise.html)
---
## Función across
- Recibe dos argumentos:
1. Selección de columnas (nombre, posición o tipo, al igual que [`select`](https://dplyr.tidyverse.org/dev/reference/dplyr_tidy_select.html))
2. Lista de funciones a aplicar sobre las columnas
```r
summarize(mpg, across(is.numeric, mean))
## # A tibble: 1 x 5
## displ year cyl cty hwy
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 3.47 2004. 5.89 16.9 23.4
```
```r
summarize(mpg, across(is.character, n_distinct))
## # A tibble: 1 x 6
## manufacturer model trans drv fl class
## <int> <int> <int> <int> <int> <int>
## 1 15 38 10 3 5 7
```
```r
summarize(mpg, across(c(year, cyl), range))
## # A tibble: 2 x 2
## year cyl
## <int> <int>
## 1 1999 4
## 2 2008 8
```
---
## Múltiples funciones
- `across` acepta varias funciones mediante una lista
```r
summarize(mpg, across(c(year, cyl), list(min, max)))
## # A tibble: 1 x 4
## year_1 year_2 cyl_1 cyl_2
## <int> <int> <int> <int>
## 1 1999 2008 4 8
```
- Es recomendable nombrar los elementos de la lista, para que las columnas de salida tengan el nombre `"columna_función"`
```r
summarize(mpg, across(is.numeric, list(media = mean, desv = sd)))
## # A tibble: 1 x 10
## displ_media displ_desv year_media year_desv cyl_media cyl_desv cty_media cty_desv hwy_media hwy_desv
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 3.47 1.29 2004. 4.51 5.89 1.61 16.9 4.26 23.4 5.95
```
---
- El formato anterior se puede cambiar modificando el parámetro opcional `.names`
```r
summarize(mpg, across(c(year, cyl), list(menor = min, mayor = max),
.names = "{fn}-{col}"))
## # A tibble: 1 x 4
## `menor-year` `mayor-year` `menor-cyl` `mayor-cyl`
## <int> <int> <int> <int>
## 1 1999 2008 4 8
```
---
## Group_by + across
- `across` no aplica sobre las variables que se usan para agrupar
```r
iris %>%
group_by(Species) %>%
summarize(across(is.numeric, mean))
## # A tibble: 3 x 5
## Species Sepal.Length Sepal.Width Petal.Length Petal.Width
## <fct> <dbl> <dbl> <dbl> <dbl>
## 1 setosa 5.01 3.43 1.46 0.246
## 2 versicolor 5.94 2.77 4.26 1.33
## 3 virginica 6.59 2.97 5.55 2.03
```
---
## Errores comunes
- `summarize` realiza las operaciones de izquierda a derecha, hay que tener cuidado cuando se combina `across` con otras operaciones:
```r
iris %>%
group_by(Species) %>%
summarize(n = n(), across(is.numeric, sd))
## # A tibble: 3 x 6
## Species n Sepal.Length Sepal.Width Petal.Length Petal.Width
## <fct> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 setosa NA 0.352 0.379 0.174 0.105
## 2 versicolor NA 0.516 0.314 0.470 0.198
## 3 virginica NA 0.636 0.322 0.552 0.275
```
```r
iris %>%
group_by(Species) %>%
summarize(across(is.numeric, sd), n = n())
## # A tibble: 3 x 6
## Species Sepal.Length Sepal.Width Petal.Length Petal.Width n
## <fct> <dbl> <dbl> <dbl> <dbl> <int>
## 1 setosa 0.352 0.379 0.174 0.105 50
## 2 versicolor 0.516 0.314 0.470 0.198 50
## 3 virginica 0.636 0.322 0.552 0.275 50
```
---
## Otras funciones de dplyr + across
`across` se puede usar también en otras funciones. Ejemplos:
* Seleccionar filas donde las columnas Ozone **y** Solar.R son `NA`
```r
filter(airquality, across(c(Ozone, Solar.R), is.na))
## Ozone Solar.R Wind Temp Month Day
## 1 NA NA 14.3 56 5 5
## 2 NA NA 8.0 57 5 27
```
```r
# version sin across, equivalente
filter(airquality, is.na(Ozone), is.na(Solar.R))
## Ozone Solar.R Wind Temp Month Day
## 1 NA NA 14.3 56 5 5
## 2 NA NA 8.0 57 5 27
```
---
* Calcular el logaritmo de todas las columnas numéricas
```r
iris %>%
mutate(across(is.numeric, log)) %>%
head()
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 1.629241 1.252763 0.3364722 -1.6094379 setosa
## 2 1.589235 1.098612 0.3364722 -1.6094379 setosa
## 3 1.547563 1.163151 0.2623643 -1.6094379 setosa
## 4 1.526056 1.131402 0.4054651 -1.6094379 setosa
## 5 1.609438 1.280934 0.3364722 -1.6094379 setosa
## 6 1.686399 1.360977 0.5306283 -0.9162907 setosa
```
---
## Operaciones de conjuntos
- `dplyr` implementa la lógica de operaciones con conjuntos sobre tibbles
+ `intersect(x,y)`: Filas que aparecen tanto en x como en y
+ `union(x,y)`: Filas que aparecen en x, en y, o en ambos
+ `setdiff(x,y)`: Filas que aparecen en x, pero no en y
```r
x <- tibble(
x1=c("A","B","C"),
x2=1:3
)
y <- tibble(
x1=c("B","C","D"),
x2=2:4
)
dplyr::intersect(x,y)
dplyr::union(x,y)
dplyr::setdiff(x,y)
```
---
## Añadir filas
- `dplyr` implementa las funciones `bind_rows` y `bind_cols` para añadir filas o columnas a un tibble, respectivamente
- En `bind_rows` las columnas se combinan por nombre y las columnas que no están en alguno de los dataframes se rellenan con NAs
```r
bind_rows(
c(a = 1, b = 2),
tibble(saludo="hola", a = 3:4, b = 5:6),
c(a = 7, b = 8)
)
## # A tibble: 4 x 3
## a b saludo
## <dbl> <dbl> <chr>
## 1 1 2 <NA>
## 2 3 5 hola
## 3 4 6 hola
## 4 7 8 <NA>
```
---
## Añadir columnas
- En `bind_cols` se unen las subtablas por posición -> todos los dataframes deben tener el mismo número de filas
+ Para unir por valores, usar `join`.
```r
# Both have to be tibbles
bind_cols(
tibble(a = 3:4, b = c("a", "b")),
tibble(logical = c(T, F))
)
## # A tibble: 2 x 3
## a b logical
## <int> <chr> <lgl>
## 1 3 a TRUE
## 2 4 b FALSE
```
</textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script src="macros.js"></script>
<script>var slideshow = remark.create({
"highlightStyle": "github",
"highlightLines": true,
"countIncrementalSlides": false
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
window.dispatchEvent(new Event('resize'));
});
(function(d) {
var s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
if (!r) return;
s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
d.head.appendChild(s);
})(document);
(function(d) {
var el = d.getElementsByClassName("remark-slides-area");
if (!el) return;
var slide, slides = slideshow.getSlides(), els = el[0].children;
for (var i = 1; i < slides.length; i++) {
slide = slides[i];
if (slide.properties.continued === "true" || slide.properties.count === "false") {
els[i - 1].className += ' has-continuation';
}
}
var s = d.createElement("style");
s.type = "text/css"; s.innerHTML = "@media print { .has-continuation { display: none; } }";
d.head.appendChild(s);
})(document);
// delete the temporary CSS (for displaying all slides initially) when the user
// starts to view slides
(function() {
var deleted = false;
slideshow.on('beforeShowSlide', function(slide) {
if (deleted) return;
var sheets = document.styleSheets, node;
for (var i = 0; i < sheets.length; i++) {
node = sheets[i].ownerNode;
if (node.dataset["target"] !== "print-only") continue;
node.parentNode.removeChild(node);
}
deleted = true;
});
})();
(function() {
"use strict"
// Replace <script> tags in slides area to make them executable
var scripts = document.querySelectorAll(
'.remark-slides-area .remark-slide-container script'
);
if (!scripts.length) return;
for (var i = 0; i < scripts.length; i++) {
var s = document.createElement('script');
var code = document.createTextNode(scripts[i].textContent);
s.appendChild(code);
var scriptAttrs = scripts[i].attributes;
for (var j = 0; j < scriptAttrs.length; j++) {
s.setAttribute(scriptAttrs[j].name, scriptAttrs[j].value);
}
scripts[i].parentElement.replaceChild(s, scripts[i]);
}
})();
(function() {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
if (/^(https?:)?\/\//.test(links[i].getAttribute('href'))) {
links[i].target = '_blank';
}
}
})();
// adds .remark-code-has-line-highlighted class to <pre> parent elements
// of code chunks containing highlighted lines with class .remark-code-line-highlighted
(function(d) {
const hlines = d.querySelectorAll('.remark-code-line-highlighted');
const preParents = [];
const findPreParent = function(line, p = 0) {
if (p > 1) return null; // traverse up no further than grandparent
const el = line.parentElement;
return el.tagName === "PRE" ? el : findPreParent(el, ++p);
};
for (let line of hlines) {
let pre = findPreParent(line);
if (pre && !preParents.includes(pre)) preParents.push(pre);
}
preParents.forEach(p => p.classList.add("remark-code-has-line-highlighted"));
})(document);</script>
<script>
slideshow._releaseMath = function(el) {
var i, text, code, codes = el.getElementsByTagName('code');
for (i = 0; i < codes.length;) {
code = codes[i];
if (code.parentNode.tagName !== 'PRE' && code.childElementCount === 0) {
text = code.textContent;
if (/^\\\((.|\s)+\\\)$/.test(text) || /^\\\[(.|\s)+\\\]$/.test(text) ||
/^\$\$(.|\s)+\$\$$/.test(text) ||
/^\\begin\{([^}]+)\}(.|\s)+\\end\{[^}]+\}$/.test(text)) {
code.outerHTML = code.innerHTML; // remove <code></code>
continue;
}
}
i++;
}
};
slideshow._releaseMath(document);
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML';
if (location.protocol !== 'file:' && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
</body>
</html>