class: center, middle, inverse, title-slide # STA 517 3.0 Programming and Statistical Computing with R ## Functionals - part 2: map vs map_dbl ### Dr Thiyanga Talagala ### 8 November 2020 --- ## Package: purrr ```r library(purrr) ``` ![](purrr.png) --- # map vs map_dbl ```r x <- list(c(1, 2, NA), c(10, 20)) x ``` ``` [[1]] [1] 1 2 NA [[2]] [1] 10 20 ``` .pull-left[ # `map` ```r map(x, mean, na.rm=TRUE) ``` ``` [[1]] [1] 1.5 [[2]] [1] 15 ``` ] .pull-right[ # `map_dbl` ```r map_dbl(x, mean, na.rm=TRUE) ``` ``` [1] 1.5 15.0 ``` ] --- # map vs map_dbl .pull-left[ # `map` ```r map(x, mean, na.rm=TRUE) ``` ``` [[1]] [1] 1.5 [[2]] [1] 15 ``` ## What is happing inside? ```r map(.x, .f, ...) ``` For each element of `.x` do .f(.x, ...) and returns a **list**. ] .pull-right[ # `map_dbl` ```r map_dbl(x, mean, na.rm=TRUE) ``` ``` [1] 1.5 15.0 ``` ## What is happing inside? ```r map_dbl(.x, .f, ...) ``` For each element of `.x` do .f(.x, ...) and returns a **numeric vector**. ] --- ![](mapviz.PNG) --- class: center, middle Slides available at: https://thiyanga.netlify.app/courses/rmsc2020/contentr/ All rights reserved by [Thiyanga S. Talagala](https://thiyanga.netlify.com/) Reference: Advanced R, Hadley Wickham