Programmazione funzionale intermedia con purrr
Colin Fay
Data Scientist & R Hacker at ThinkR
Una funzione:
Un numero $n$:
Un vettore di caratteri $z$
Quando è una funzione, .f può essere:
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: funzione anonima con formula unilaterale
# Con un parametro
map_dbl(visits2017, ~ round(mean(.x)))
# È equivalente a
map_dbl(visits2017, ~ round(mean(.)))
# È equivalente a
map_dbl(visits2017, ~ round(mean(..1)))
mapper: funzione anonima con formula unilaterale
# Con due parametri
map2(visits2016, visits2017, ~ .x + .y)
# È equivalente a
map2(visits2016, visits2017, ~ ..1 + ..2)
# Con più di due parametri
pmap(list, ~ ..1 + ..2 + ..3)
as_mapper(): crea oggetti mapper da una funzione lambda
# Funzione classica
round_mean <- function(x){
round(mean(x))
}
# Come mapper
round_mean <- as_mapper(~ round(mean(.x))))
I mapper sono:
Concisi
Facili da leggere
Riutilizzabili

Programmazione funzionale intermedia con purrr