名前のないリストの操作

purrr で学ぶ関数型プログラミング入門

Auriel Fournier

Instructor

まずはパイプについて

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

以下の記述が不要になります

output1 <- function_one()

output <- function_two(output1)
purrr で学ぶ関数型プログラミング入門

リストに名前はありますか?

パイプなし

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

パイプあり

survey_data %>%
        names()
"LakeErieS" "LakeErieN" "LakeErieW" "LakeErieE"
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"
purrr で学ぶ関数型プログラミング入門

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
purrr で学ぶ関数型プログラミング入門

練習しましょう!

purrr で学ぶ関数型プログラミング入門

Preparing Video For Download...