이름 없는 리스트 다루기

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...