map()의 다양한 변형

purrr로 배우는 함수형 프로그래밍 기초

Auriel Fournier

Instructor

리스트 외 출력

# Map over survey_data and determine number of rows
map(survey_data, ~nrow(.x))
$`LakeErieS`
[1] 14

$`LakeErieN`
[1] 14

$`LakeErieW`
[1] 14

$`LakeErieE`
[1] 15
purrr로 배우는 함수형 프로그래밍 기초

purrr::map_variants

리스트 출력

# Determine row number
map(survey_data, ~nrow(.x))

   

     

더블(숫자형의 일종)

# Determine row number
map_dbl(survey_data, ~nrow(.x))
$`LakeErieS`
[1] 14

$`LakeErieN`
[1] 14

$`LakeErieW`
[1] 14

$`LakeErieE`
[1] 15
14 14 14 15
purrr로 배우는 함수형 프로그래밍 기초

map_lgl()

리스트

# Determine row number
map(survey_data, ~nrow(.x))

 

 

논리형

# Determine if elements have 14 rows
map_lgl(survey_data, ~nrow(.x)==14)
$`LakeErieS`
[1] 14

$`LakeErieN`
[1] 14

$`LakeErieW`
[1] 14

$`LakeErieE`
[1] 15
TRUE TRUE TRUE FALSE
purrr로 배우는 함수형 프로그래밍 기초

map_chr()

리스트 출력

# Map over species_names list
map(species_names, ~.x)

 

 

문자형

# Map over species_names list
map_chr(species_names, ~.x)
$`LakeErieS`
[1] "Green Frog"

$`LakeErieN`
[1] "American Bullfrog"

$`LakeErieW`
[1] "Gray Treefrog"

$`LakeErieE`
[1] "Mudpuppy"
LakeErieS       LakeErieN 
"Green Frog"    "American Bullfrog" 
LakeErieW       LakeErieE 
"Gray Treefrog" "Mudpuppy"
purrr로 배우는 함수형 프로그래밍 기초

예제 실습!

# Create a data frame called survey_rows
survey_rows <- data.frame(names = names(survey_data),
                       rows = NA)

# Map over survey_data to determine row number in each element survey_rows$rows <- map_dbl(survey_data, ~nrow(.x))
# Print out the survey_rows data frame survey_rows
    names       rows
1   LakeErieS   14
2   LakeErieN   14
3   LakeErieW   14
4   LakeErieE   15
purrr로 배우는 함수형 프로그래밍 기초

Let's purrr-actice!

purrr로 배우는 함수형 프로그래밍 기초

Preparing Video For Download...