R-bloggers

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Using purrr’s map family functions in dplyr::mutate

Posted on September 5, 2018 by yoshidk6 in R bloggers | 0 Comments

[This article was first published on R - yoshidk6’s blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

map family functions of the purrr package are very useful for using non-vectorized functions in dplyr::mutate chain (see GitHub – jennybc/row-oriented-workflows: Row-oriented workflows in R with the tidyverse or https://www.jimhester.com/2018/04/12/vectorize/).
I encounter the needs for this especially when dealing with nested data frames.

One of the drawbacks is that name/input argument assignments become confusing when you want to use more than two columns of your data frames (and using pmap family) for the function of interest. This post first briefly review how mutate works in combination with map or map2 , then provide two approaches to avoid confusions around name assignments when using pmap .