การทำงานกับลิสต์ที่ไม่มีชื่อ

Foundations of Functional Programming with purrr

Auriel Fournier

Instructor

ก่อนอื่น มารู้จัก pipe กัน

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

แทนที่จะต้องเขียน

output1 <- function_one()

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

ลิสต์มีชื่อหรือเปล่า?

แบบไม่ใช้ Pipe

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

แบบใช้ Pipe

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

ยังไม่มีชื่อ? กำหนดเลย!

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

การใช้ pipe ภายใน 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

มาฝึกกันเถอะ!

Foundations of Functional Programming with purrr

Preparing Video For Download...