Travailler avec des listes sans noms

Fondements de la programmation fonctionnelle avec purrr

Auriel Fournier

Instructor

Mais d'abord, les pipes

output <-  function_one() %>%
            function_two()

Au lieu de devoir

output1 <- function_one()

output <- function_two(output1)
Fondements de la programmation fonctionnelle avec purrr

Ma liste a-t-elle des noms ?

Sans pipes

names(survey_data)
"LakeErieS" "LakeErieN" "LakeErieW" "LakeErieE"

Avec pipes

survey_data %>%
        names()
"LakeErieS" "LakeErieN" "LakeErieW" "LakeErieE"
Fondements de la programmation fonctionnelle avec purrr

Pas de noms ? Attribuez-en !

library(repurrrsive)
data(sw_films)
str(sw_films)
List of 14
 $ title        : chr "A New Hope"
 $ episode_id   : int 4
 $ opening_crawl: chr "It is a period of ..."
 $ director     : chr "George Lucas"
 ...
sw_films <- sw_films %>%
  set_names(map_chr(sw_films, "title"))
names(sw_films)
[1] "A New Hope"          "Attack of the Clones"   
[3] "The Phantom Menace"  "Revenge of the Sith"    
[5] "Return of the Jedi"  "The Empire Strikes Back"
[7] "The Force Awakens"
Fondements de la programmation fonctionnelle avec purrr

Des pipes dans map()

waterfowl_data

 

$LakeErieS
[1] 0 0 10 5

$LakeErieN
[1] 0 0 1000 5

$LakeErieW
[1] 10000 0 0 1

$LakeErieE
[1] 10 10 5 0
map(waterfowl_data, ~.x %>% 
                sum() %>% 
                log())
$LakeErieS
[1] 2.70805

$LakeErieN
[1] 6.912743

$LakeErieW
[1] 9.21044

$LakeErieE
[1] 3.218876
Fondements de la programmation fonctionnelle avec purrr

Passons à la pratique !

Fondements de la programmation fonctionnelle avec purrr

Preparing Video For Download...