purrr
How to use map from purrr with dplyr::mutate to create multiple new columns based on column pairs
Here is one option with purrr. We get the unique prefix of the names of the dataset (‘nm1’), use map (from purrr) to loop through the unique names, select the column that matches the prefix value of ‘nm1’, add the rows using reduce and the bind the columns (bind_cols) with the original dataset library(tidyverse) nm1 … Read more
Why use purrr::map instead of lapply?
If the only function you’re using from purrr is map(), then no, the advantages are not substantial. As Rich Pauloo points out, the main advantage of map() is the helpers which allow you to write compact code for common special cases: ~ . + 1 is equivalent to function(x) x + 1 (and \(x) x … Read more
What is meaning of first tilde in purrr::map
As per the map help documentation, map needs a function but it also accepts a formula, character vector, numeric vector, or list, the latter of which are converted to functions. The ~ operator in R creates formula. So ~ lm(mpg ~ wt, data = .) is a formula. Formulas are useful in R because they … Read more