Working with unnamed lists

Foundations of Functional Programming with purrr

Auriel Fournier

Instructor

But first, pipes

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

Instead of needing

output1 <- function_one()

output <- function_two(output1)
Foundations of Functional Programming with purrr

Does my list have names?

Without Pipes

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

With Pipes

survey_data %>%
        names()
"LakeErieS" "LakeErieN" "LakeErieW" "LakeErieE"
Foundations of Functional Programming with purrr

No names? Set some!

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"
Foundations of Functional Programming with purrr

Pipes within 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
Foundations of Functional Programming with purrr

Let's purrr-actice!

Foundations of Functional Programming with purrr

Preparing Video For Download...