Intermediate Functional Programming with purrr
Colin Fay
Data Scientist & R Hacker at ThinkR
Funkce:
Číslo $n$:
Znakový vektor $z$
Pokud je .f funkcí, může být:
my_fun <- function(x) {
round(mean(x))
}
map_dbl(visit_2014, my_fun)
[1] 5526 6546 6097 7760
[5] 7025 7162 10484 8256
[9] 6558 7686 5723 5053
map_dbl(visit_2014, function(x) {
round(mean(x))
})
[1] 5526 6546 6097 7760
[5] 7025 7162 10484 8256
[9] 6558 7686 5723 5053
mapper: anonymní funkce s jednostrannou formulí
# S jedním parametrem
map_dbl(visits2017, ~ round(mean(.x)))
# Je ekvivalentní s
map_dbl(visits2017, ~ round(mean(.)))
# Je ekvivalentní s
map_dbl(visits2017, ~ round(mean(..1)))
mapper: anonymní funkce s jednostrannou formulí
# Se dvěma parametry
map2(visits2016, visits2017, ~ .x + .y)
# Je ekvivalentní s
map2(visits2016, visits2017, ~ ..1 + ..2)
# S více než dvěma parametry
pmap(list, ~ ..1 + ..2 + ..3)
as_mapper(): vytvoření mapper objektů z lambda funkce
# Klasická funkce
round_mean <- function(x){
round(mean(x))
}
# Jako mapper
round_mean <- as_mapper(~ round(mean(.x))))
Mappery jsou:
Stručné
Přehledné
Znovupoužitelné

Intermediate Functional Programming with purrr