Even more complex problems

Foundations of Functional Programming with purrr

Auriel Fournier

Instructor

What if the values you want are buried?

library(repurrrsive)
data(gh_repos)

forks <- gh_repos %>% 
  map( ~map(.x, "forks"))

forks
[[1]]
[[1]][[1]]
[1] 0

[[1]][[2]]
[1] 1

[[1]][[3]]
[1] 0

[[1]][[4]]
[1] 0
Foundations of Functional Programming with purrr

Summary stats in purrr

# Create an empty data frame
bird_df <- data.frame(weight=NA, 
        wing_length=rep(NA, 4))

# Extract the bird names
bird_df$taxa <- 
        names(bird_measurements)

# For loop to pull out each species 
for(i in 1:4){
bird_df[i,] <- 
        bird_measurements[[i]]  
    }
summary(bird_df)
Foundations of Functional Programming with purrr

Summary stats

bird_measurements %>% 
  map_df(~ data_frame(
  weight = .x[["weight"]],
  wing_length = .x[["wing length"]],
  taxa = "bird")) %>%
     select_if(is.numeric)  %>%
     summary(.x)

$weight
  Min. 1st Qu.  Median    
  7.00   59.12   86.45 

  Mean 3rd Qu.    Max.
  69.97   97.30  100.00 

$wing_length
  Min. 1st Qu.  Median    
  12.00   29.25   55.00   

  Mean 3rd Qu.    Max. 
  63.00   88.75  130.00
Foundations of Functional Programming with purrr

Let's purrr-actice!

Foundations of Functional Programming with purrr

Preparing Video For Download...