Introduction to Programming with purrr

Intermediate Functional Programming with purrr

Colin Fay

Data Scientist & R Hacker at ThinkR

$whoami

Instructor Colin Fay drinking coffee

Intermediate Functional Programming with purrr

Discovering purrr

Intermediate Functional Programming with purrr

What will this course cover?

map basic skeleton

From: Charlotte Wickham — A introduction to purrr

Intermediate Functional Programming with purrr

Open Data Portal website from St. Malo, France

Intermediate Functional Programming with purrr

purrr basics - a refresher (Part 1)

map(.x, .f, ...)

  • for each element of .x
  • do .f(.x, ...)
  • return a list
res <- map(visit_2015, sum) 
class(res)
"list"

map_dbl(.x, .f, ...)

  • for each element of .x
  • do .f(.x, ...)
  • return a numeric vector
res <- map_dbl(visit_2015, sum) 
class(res)
"numeric"
Intermediate Functional Programming with purrr

purrr basics - a refresher (Part 2)

map2(.x, .y, .f, ...)

  • for each element of .x and .y
  • do .f(.x, .y, ...)
  • return a list
res <- map2(visit_2015,
            visit_2016,
            sum)
class(res)
"list"

map2_dbl(.x, .f, ...)

  • for each element of .x and .y
  • do .f(.x, .y, ...)
  • return a numeric vector
res <- map2_dbl(visit_2015,
                visit_2016,
                sum)
class(res)
"numeric"
Intermediate Functional Programming with purrr

purrr basics - a refresher (Part 3)

pmap(.l, .f, ...)

  • for each sublist of .l
  • do f(..1, ..2, ..3, [etc], ...)
  • return a list
l <- list(visit_2014,
          visit_2015,
          visit_2016)
res  <- pmap(l, sum)
class(res)
"list"

pmap_dbl(.l, .f, ...)

  • for each sublist of .l
  • do f(..1, ..2, ..3, [etc], ...)
  • return a numeric vector
l <- list(visit_2014,
          visit_2015,
          visit_2016)
res <- pmap_dbl(l, sum)
class(res)
"numeric"
Intermediate Functional Programming with purrr

Let's practice!

Intermediate Functional Programming with purrr

Preparing Video For Download...