-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtidyverse.qmd
1195 lines (870 loc) · 26.1 KB
/
tidyverse.qmd
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
---
title: "Tidyverse"
author: "Uroš Godnov"
format:
revealjs:
theme: simple
slide-number: true
embed-resources: true
preview-links: auto
footer: "uros.godnov@gmail.com"
transition: slide
background-transition: fade
code-line-numbers: true
execute:
echo: true
warning: false
---
```{r}
#| echo: false
library(tidyverse)
library(tidyr)
library(knitr)
library(readxl)
library(broom)
library(estimatr)
library(gt)
library(glue)
library(lubridate)
```
# Tidyverse
## Tidyverse - 1
- the tidyverse is an opinionated collection of R packages designed for data science
- all packages share an underlying philosophy ('tidy') and common APIs
```{r}
#| echo: false
include_graphics("./Pictures/tidyverse1.jpg")
```
## Tidyverse - 2
```{r}
#| echo: false
include_graphics("./Pictures/tidyverse2.jpg")
```
## Tidyverse - 3
```{r}
#| echo: false
include_graphics("./Pictures/tidyverse3.jpg")
```
## Tidyverse - 4
```{r}
#| echo: false
include_graphics("./Pictures/tidyverse4.jpg")
```
## Dplyr
- Hadley Wickham
- transforming data
- writing grammar with pipe operator (%>%)
- pipe operator in magrittr
- spread and gather in tidyr
## Grammar - 1
- select: return a subset of the columns of a data frame, using a flexible notation
- filter: extract a subset of rows from a data frame based on logical conditions
- arrange: reorder rows of a data frame
- rename: rename variables in a data frame
## Grammar - 2
- group_by() takes an existing tbl and converts it into a grouped tbl where operations are performed "by group". ungroup() removes grouping
- mutate: add new variables/columns or transform existing variables
- summarise / summarize: generate summary statistics of different variables in the data frame, possibly within strata
- %>%: the “pipe” operator is used to connect multiple verb actions together into a pipeline
## Select - 1
- select(data.frame, columns)
- select(data.frame, column1:column3) – selects col1, col2, col3
- select(data.frame, -(col1:col3)) – selects all columns except col1 to col3
- starts_with() and ends_with()
- contains()
## Select - 2 {.smaller}
- using across to specify conditions
- in the past select_if, select_all
```{r}
#| echo: true
df1<-iris%>%select_if(is.factor)
head(df1,2)
df1<-iris%>%select(where(is.factor))
head(df1,2)
df2<-df1%>%select_all(toupper)
head(df2,2)
df2 <- df1 %>%
rename_with(toupper, everything())
head(df2,2)
```
## Complex select - 1
- where replaces if
- combining where with any_of or all_of
```{r}
df1<-iris%>%select(where(is.factor))
head(df1,2)
```
## Complex select - 2 {.smaller}
- where & any_of
```{r}
#| echo: False
df <- tibble(
name = c("Alice", "Bob", "Charlie"),
age = c(23, 35, 45),
salary = c(50000, 60000, 70000),
department = c("Sales", "Marketing", "IT")
)
df
```
```{r}
interested_cols <- c("age", "salary", "experience")
# Select columns that are numeric and whose names are in interested_cols
result <- df %>%
select(
where(is.numeric) & any_of(interested_cols)
)
result
```
## Complex select - 3 {.smaller}
- where & all_of
- all_of is strict
```{r}
#| error: True
interested_cols <- c("age", "salary", "experience")
# Select columns that are numeric and whose names are in interested_cols
result <- df %>%
select(
where(is.numeric) & all_of(interested_cols)
)
```
## %>%
- pipe operator
- stringing together multiple dplyr functions in a sequence of operations
first(x) %>% second %>% third
- from R 4.1 you have native pipe |>
## Arrange
- orders dataframe according to variables
- arrange(data.frame, col1)
- arrange(data.frame, desc(col1))
```{r}
iris%>%select(contains("Sepal"))%>%arrange(desc(Sepal.Width))%>%head(3)
```
## Rename - 1
- renames columns
- rename(dataframe, newcol1=col1,newcol2=col2)
- new name is on the left side
```{r}
iris%>%select(contains("Sepal"))%>%arrange(desc(Sepal.Width))%>%
rename(SepalW=Sepal.Width,
SepalL=Sepal.Length)%>%head(3)
```
## Rename - 2
- rename_with(.data, .fn, .cols = everything(), ...)
```{r}
colName<-function(x) {
tmp<-stringr::str_sub(x,1,7)
return(gsub("\\.","",tmp))}
iris %>%
rename_with(colName, contains("Sepal")) %>%
head(3)
```
## Rename (advanced) - 3 {.smaller}
```{r}
#| echo: False
df <- tibble(
old_name1 = c(1, 2, 3),
old_name2 = c("A", "B", "C"),
old_name3 = c(TRUE, FALSE, TRUE)
)
name_mapping_df <- tibble(
old_name = c("old_name1", "old_name2", "old_name3"),
new_name = c("new_name1", "new_name2", "new_name3")
)
df
name_mapping_df
```
```{r}
df_renamed <- df %>%
rename_with(
~ name_mapping_df$new_name[match(.x, name_mapping_df$old_name)],
.cols = everything()
)
df_renamed
```
## Summarise - 1 {.smaller}
- create one or more scalar variables summarizing the variables of an existing tb
- powerfull with group_by clause
```{r}
iris%>%select(-Species)%>%
summarise(numberOfRows=n())
iris%>%select(-Species)%>%
summarise(median=median(Petal.Length),
mean=mean(Petal.Length))
```
## Summarise - 2 {.smaller}
- summarise_at(): the input is character vector
- summarise_if(): using predicate function
- superseded with across function in dplyr v1.0.0
```{r}
columns<-c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
iris%>%summarise_at(columns,.funs=list(mean, median))
iris%>%summarise_if(is.numeric,.funs=list(mean, median))
```
## Summarise - 3 {.smaller}
- across: applies funtion(s) to a set of colummns
- across(.cols = everything(), .fns = NULL, ..., .names = NULL)
```{r}
columns<-c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
iris%>%summarise(across(.cols=all_of(columns),.fns=list(mean, median)))
iris%>%summarise(across(.cols=is.numeric,.fns=list(mean, median)))
```
## Mutate - 1 {.smaller}
- computes new variables
- related verb transmute() which drops non transformed variables
```{r}
iris %>%
mutate(AverageLength=(Sepal.Length+Petal.Length)/2,
AverageWidth=(Sepal.Width+Petal.Width)/2)%>%
head(3)
```
## Mutate - 2 {.smaller}
- mutate_at, mutate_if
- mutate+across
```{r echo=TRUE}
iris%>%mutate_if(is.numeric, as.character)%>%head(.,3)
iris%>%mutate(across(.cols=is.numeric, .fns=as.character))%>%head(3)
```
## Lab
- open dplyr.txt and complete the excercises
## Group_by - 1
- used to generate summary statistics from the data frame within strata defined by a variable
```{r}
iris%>%group_by(Species)%>%summarise(across(.fns=list(mean, median)))
```
## Group_by - 2
- what do we want, best seller overall or by region?
```{r}
#| echo: False
sales_data <- tibble(
salesperson = c('Alice', 'Bob', 'Catherine', 'Alice', 'Bob', 'Catherine', 'Alice', 'Bob', 'Catherine'),
region = c('East', 'East', 'East', 'West', 'West', 'West', 'North', 'North', 'North'),
sales = c(300, 250, 450, 500, 400, 550, 600, 350, 500)
)
sales_data
```
## Group_by - 3 {.smaller}
- using .by instead of group_by()
- .by works only for current summarization
```{r}
#| code-line-numbers: "2"
sales_data %>%
group_by(region) %>%
summarise(total_sales = sum(sales))
```
```{r}
#| code-line-numbers: "3"
sales_data %>%
summarise(total_sales = sum(sales),
.by="region")
```
## Slice helpers - 1 {.smaller}
- top_n(), sample_n(), sample_frac()
- v1.0.0: slice_min(), slice_max(), slice_head(), slice_tail(), slice_sample()
```{r}
iris%>%top_n(2, wt=Petal.Width)
iris%>%slice_max(order_by = Petal.Width, n=2)
```
## Slice helpers - 2 {.smaller}
- without ties
```{r}
iris%>%slice_max(order_by = Petal.Width, n=2, with_ties=FALSE)
```
- random selection
```{r}
iris%>%sample_frac(size=0.03)
```
## Slice helpers - 3 {.smaller}
- random selection with slice_sample()
```{r }
iris%>%slice_sample(prop=0.03)
```
## Lab
- import Master.csv
- number of players by year of birth n()
- number of players per birthCountry (asc)
- average weight in kg per birthCountry (desc) where number of per birthcountry>5
help:
- select birthyear
- group by
- summarise (use function n())
- arrange
## Window rank functions - 1{.smaller}
- row_number
- ntile
- min_rank
- dense_rank
```{r}
df<-iris%>%slice_sample(n=10)
df%>%mutate(id=row_number(),
groups=ntile(Species,5),
minrank=min_rank(Species),
denserank=dense_rank(Species))%>%
select(-contains("Petal"),-contains("Sepal"),)
```
## Window rank functions - 2 {.smaller}
- how to add row_numbers in every group by decreasig size of Petal.Length
- how to get the second largest?
```{r }
df<-iris%>%slice_sample(n=10)
df%>%group_by(Species)%>%arrange(desc(Petal.Length))%>%
mutate(id=row_number())%>%
ungroup()%>%
select(Petal.Length, Species, id)%>%arrange(Species)
```
## Lab
- import Master.csv
- show the second heaviest man from the first 5 contries with the largest average weight in kg per birthCountry (desc) where number of per birthcountry>5
## Pivot_longer and pivot_wider (tidyr) - 1 {.smaller}
- successors to spread and gather
- pivot_wider
```{r echo=TRUE}
df<-airquality%>%select(Month, Day, Temp)%>%dplyr::filter(Month %in% c(5,6) & Day<4)%>%
pivot_wider(names_prefix = "Month ", names_from=Month, values_from=Temp)
df
```
## Pivot_longer and pivot_wider (tidyr) - 2 {.smaller}
- pivot_longer
```{r}
df<-data.frame(day=c("Monday", "Tuesday","Wednesday"),month_aug=c(46,76,32),
month_sep=c(62,67,23), month_oct=c(43,NA,31))
df
```
```{r}
df%>%pivot_longer(cols=month_aug:month_oct, names_to = "Month",
values_to = "Temperature")%>%
head(6)
```
## Pivot_longer and pivot_wider (tidyr) - 3 {.smaller}
- using names_prefix and values_drop_na
```{r}
df %>%
pivot_longer(cols=month_aug:month_oct,names_prefix="month_",
names_to = "Month", values_to = "Temperature", values_drop_na=TRUE) %>%
head(6)
```
## Pivot_longer and pivot_wider (tidyr) - 4 {.smaller}
- using pivot_longer with values_transform
```{r}
#| echo: False
df <- tibble(
name = c("Alice", "Bob", "Charlie"),
height_cm = c(170, 180, 175),
weight_kg = c(65, 75, 68),
class=c("A","A","B")
)
df
```
```{r}
#| error: True
df_long <- df %>%
pivot_longer(
cols = c(height_cm, weight_kg, class),
names_to = "measurement_type",
values_to = "value"
)
df_long
```
## Pivot_longer and pivot_wider (tidyr) - 5
```{r}
#| code-line-numbers: "6"
df_long <- df %>%
pivot_longer(
cols = c(height_cm, weight_kg, class),
names_to = "measurement_type",
values_to = "value",
values_transform = list(value=as.character)
)
df_long
```
## Lab
- import norway_new_car_sales_by_model.xlsx
- show the number of sold cars by manufacturer (rows) and years(columns)
- use spread and pivot_wider
## fill() - 1
- carry-forward function
```{r}
data <- tibble(
time = 1:5,
measurement = c(NA, 3, NA, NA, 5)
)
data
```
## fill() - 2
```{r}
filled_data <- data %>%
fill(measurement, .direction = "down")
filled_data
```
## User defined functions in dplyr - 1 {.smaller}
- using in mutate
- function has to be vectorized; if not, use rowwise() to go row by row
- vectorized function
```{r}
fTOc<-function(x) {round((x-32)*5/9,0)}
input<-c(100, 102, 99)
fTOc(input)
```
- nonvectorized function
```{r}
stupidFunction<-function(x,y){return(sum(x,y))}
airquality%>%mutate(something=stupidFunction(Month, Day))%>%head(3)
```
## User defined functions in dplyr - 2 {.smaller}
- using rowwise() operation
```{r }
airquality%>%rowwise()%>%mutate(something=stupidFunction(Month, Day))%>%head(3)
```
- or add id and use group_by(id)
```{r}
airquality%>%mutate(id=row_number())%>%group_by(id)%>%
mutate(something=stupidFunction(Month, Day))%>%ungroup()%>%head(3)
```
## User defined functions in dplyr - 3 {.smaller}
- use Vectorize() function
```{r}
stupidFunction_v<-Vectorize(stupidFunction)
airquality%>%
mutate(something=stupidFunction_v(Month, Day))%>%head(5)
```
## Lab
- use Titanic dataset
- write a function, which will return the following:
- if a person was a child and didn't survive, the function should return "Poor child"
- if a person was a woman and didn't survive, the function should return "Oh no"
- if a person was an adult man and did survive, the function should return "You shouldn't save you if there were still women and children aboard"
## Lab
- take dataset mtcars
- write a function which will tranform mpg to l/100 kms and assign values to a new column consumption
- display mean and median for mpg, hp, consumption
- identify the worst and best economic car
## Join - 1
- inner_join
- left_join
- right_join
- full_join
## Join - 2
```{r}
#| echo: false
include_graphics("./Pictures/join.jpg")
```
## Join - 3
- semi_join
- anti_join
```{r}
#| echo: false
include_graphics("./Pictures/semijoin.jpg")
```
## Examples {.smaller}
```{r}
band_members%>%head()
band_instruments%>%head()
```
## Examples {.smaller}
```{r}
band_members %>% inner_join(band_instruments)
band_members %>% left_join(band_instruments)
```
## Examples {.smaller}
```{r echo=TRUE}
band_members %>% right_join(band_instruments)
band_members %>% full_join(band_instruments)
```
## Examples {.smaller}
- A semi join differs from an inner join because an inner join will return one row of x for each matching row of y, where a semi join will never duplicate rows of x
```{r}
band_members %>% semi_join(band_instruments)
band_members %>% anti_join(band_instruments)
```
## Joining by many columns, different names - 1 {.smaller}
- inner_join(df1, df2, by=c("a1"="b2","z3"="u1"))
```{r}
d1 <- data_frame(
x = letters[1:3],
y = LETTERS[1:3],
a = rnorm(3)
)
d2 <- data_frame(
x2 = letters[3:1],
y2 = LETTERS[3:1],
b = rnorm(3)
)
d1 %>%
left_join(d2, by = c("x" = "x2", "y" = "y2"))
```
## Joining by many columns, different names - 2
- new syntax from dplyr v1.1.0
- more robust syntax
```{r}
#| code-line-numbers: "3"
d1 %>%
left_join(d2,
by=join_by(x == x2, y == y2))
```
## Lab
- download csv 2019 version of data from http://www.seanlahman.com/baseball-archive/statistics/
- extract master.csv and FieldingOF.csv
- meta data are available at http://www.seanlahman.com/files/database/readme2019.txt
- Display the name (firstname+lastname) of the player who had the second highest number of games played in center field for each year from 1990 to 2000
## Lag and lead {.smaller}
- find the "previous" (lag()) or "next" (lead()) values in a vector
```{r}
r<-data.frame(year=2005:2014,population=sample(14000:15000, 10, replace=T))
r<-cbind(r,lag(r$population))
names(r)<-c("year","pop","pop1")
r%>%
mutate(index=round(100*(1+(pop-pop1)/pop1),2))%>%
select(year,pop,pop1,index)
```
## Nested ifelse or case_when - 1 {.smaller}
- nested ifelse or tidyverse if_else
```{r}
#| echo: False
people <- tibble(
name = c('Alice', 'Bob', 'Catherine', 'David', 'Emma'),
age = c(25, 58, 33, 15, 46)
)
people
```
```{r}
people <- people %>%
mutate(
age_group = if_else(
age < 18, 'Child',
if_else(
age <= 35, 'Young Adult',
if_else(
age <= 60, 'Adult',
'Senior' # Fallback condition if none of the above are TRUE
)
)
)
)
people
```
## Nested ifelse or case_when - 2 {.smaller}
- case_when
```{r}
#| code-line-numbers: "3|4-6|7"
people <- people %>%
mutate(
age_group = case_when(
age < 18 ~ 'Child',
age <= 35 ~ 'Young Adult',
age <= 60 ~ 'Adult',
TRUE ~ 'Senior' # Fallback condition if none of the above are TRUE
)
)
# View the updated data
people
```
## Nested ifelse or case_when - 3 {.smaller}
- newer syntax
- .default + "="
```{r}
#| code-line-numbers: "7"
people <- people %>%
mutate(
age_group = case_when(
age < 18 ~ 'Child',
age <= 35 ~ 'Young Adult',
age <= 60 ~ 'Adult',
.default = 'Senior' # Fallback condition if none of the above are TRUE
)
)
# View the updated data
people
```
## Unite and separate - 1 {.smaller}
- unite combines multiple columns into a single column
```{r}
data<-as.data.frame(HairEyeColor)
head(data,4)
```
- uniting Hair, Eye and Sex into Properties column
```{r}
data%>%unite("Properties",Hair:Sex, sep="/")%>%
head(4)
```
## Unite and separate - 2 {.smaller}
- separate turns a single character column into multiple columns
- splitting the values of the column wherever a separator character appears
```{r}
df<-data%>%unite("Properties",Hair:Sex, sep="/")
df%>%separate(Properties, into=c("A","B","C"),sep="/")%>%
head(3)
```
- what about in this case?
```{r}
df<-data.frame(name=c("Sam Jones Sr. Mr.",
"Lady Gaga Singer",
"Valentino Rossi Mr. (Junior) son")); df
```
## Unite and separate - 3 {.smaller}
- separate has extra atribute, which decides, what to with "extra data"
```{r}
df%>%separate(name, into=c("First_name","Last_Name"), extra="drop")
```
- fill: what happens when there are not enough pieces
```{r}
df%>%separate(name,
into=c("First_name","Last_Name","Something","Title"), extra="drop", fill="left")
```
## Nest and unnest {.smaller}
- nesting creates a list-column of data frames
- nesting is implicitly a summarising operation
- convenient for bulding the models
```{r}
df<-iris%>%nest(-Species)
df
```
```{r}
df%>%unnest()%>%head(2)
```
## Lab {.smaller}
```
1. Take airmiles dataset and transform dataframe to a column with name Airmiles.
Add a new column called Year from 1937 to 1960.
Calculate index for Airmiles. Which year has the highest index.
2. Take dates_df dataframe and split date column into columns month, day and year!
dates_df <- data.frame(date = c("5/24/1930",
"5/25/1930",
"5/26/1930",
"5/27/1930",
"5/28/1930"),
stringsAsFactors = FALSE)
3. Create 100 random numbers between 1 and 95 and transform it to a column Age in a dataframe
People.
Discretize Age into the following age groups:
- 1:20
- 21:45
- 46:55
- 56:70
- 71:
Calculate summary statistics.
```
## map family functions - 1 {.smaller}
- purrr package
- map familiy functions transform their input by applying a function to each element of a list or atomic vector and returning an object of the same length as the input
- map(): returns a list
- map_lgl(), map_int(), map_dbl() and map_chr() return an atomic vector of the indicated type
- map_df(), map_dfr() and map_dfc() return a data frame created by row-binding and column-binding
- there are also map_if and map_at functions, where logic is the same as with _if and _at functions till now.
```{r}
airquality%>%select(Ozone)%>%
map(mean)
```
## map family functions - 2 {.smaller}
- one function, no arguments: map
- one function with 2 arguments: map2
- one function with many arguments: pmap
## map family functions - 3 {.smaller}
- passing a parameter
```{r}
airquality%>%select(Temp)%>%
map(mean)
```
## map family functions - 4 {.smaller}
- reading files with the same structure, map_df
```{r}
files<-list.files(pattern = "*.csv", path = "./Mtcars/", full.names = TRUE)
allDF<-map_df(files, read.csv2)
```
- calling more than 1 function in map
- invoke, invoke_map: retired
```{r}
airquality%>%select(Ozone)%>%invoke_map(.f=c("mean", "max"),na.rm=TRUE)
```
## map family functions - 5 {.smaller}
```{r}
airquality%>%select(Month, Day)%>%mutate(Something=map2_int(Month,Day,sum))%>%
head(3)%>%as_tibble()
airquality%>%select(Month, Day)%>%mutate(Something=map2_chr(Month,Day,sum))%>%
head(3)%>%as_tibble()
```
## map family functions - 6 {.smaller}
- map handles vectorization
```{r}
stupidFunction<-function(x,y){return(sum(x,y))}
airquality%>%select(Month, Day)%>%
mutate(Something=map2_chr(Month,Day,stupidFunction))%>%
head(3)%>%as_tibble()
```
## map family functions - 7 {.smaller}
- pmap: arbitrary number of arguments
```{r}
airquality%>%select(Wind,Temp, Month, Day)%>%
mutate(Something=pmap_dbl(list(Wind,Temp,Month,Day),sum))%>%
head(3)%>%as_tibble()
airquality%>%select(Wind,Temp, Month, Day)%>%
mutate(Something=pmap_chr(list(Wind,Temp,Month,Day),sum))%>%
head(3)%>%as_tibble()
```
## map family functions - 8 {.smaller}
- passing additional arguments to function
```{r}
airquality%>%select(Ozone,Day)%>%dplyr::filter(is.na(Ozone))%>%
mutate(Something=map2_int(Ozone,Day,max))%>%
head(3)%>%as_tibble()
airquality%>%select(Ozone,Day)%>%dplyr::filter(is.na(Ozone))%>%
mutate(Something=map2_int(Ozone,Day,max, na.rm=TRUE))%>%
head(3)%>%as_tibble()
```
## map family functions - 9 {.smaller}
- passing more than one argument to called function
```{r}
files<-list.files(pattern = "*.csv", path = "./Mtcars/", full.names = TRUE)
allDF<-map_df(files, read.csv2, skip=1,header=FALSE)
```
## map family functions - 10
- progress bar
```{r}
#| eval: False
x<-map(1:100,\(x) Sys.sleep(0.1), .progress="TRUE")
```
## map family functions - 11
- map always returns a list
- we can control behavior with map_vec