在工作流中使用 purrr

使用 purrr 掌握函数式编程基础

Auriel Fournier

Instructor

检查名称

library(repurrrsive)
data(sw_films)
names(sw_films)
NULL
str(sw_films)
List of 14
 $ title        : chr ...
 $ episode_id   : int ...
 $ opening_crawl: chr ...
 $ director     : chr ...
 ...
sw_films <- sw_films %>%
  set_names(map_chr(sw_films, 
            "title"))

names(sw_films)
[1] "A New Hope"              
[2] "Attack of the Clones"   
[3] "The Phantom Menace"      
[4] "Revenge of the Sith"    
[5] "Return of the Jedi"      
[6] "The Empire Strikes Back"
[7] "The Force Awakens"
使用 purrr 掌握函数式编程基础

在提问时设置名称

map_chr(sw_films, ~.x[["episode_id"]]) %>%
  set_names(map_chr(sw_films, "title")) %>%
  sort()
  The Phantom Menace    Attack of the Clones 
                    "1"                     "2" 
    Revenge of the Sith              A New Hope 
                    "3"                     "4" 
The Empire Strikes Back      Return of the Jedi 
                    "5"                     "6" 
      The Force Awakens 
                    "7"
使用 purrr 掌握函数式编程基础

让我们来 purrr 练习!

使用 purrr 掌握函数式编程基础

Preparing Video For Download...