I'm building a function that you provide a dataframe and a vector of variable names that are numeric - and i want it to return a dataframe with a corresponding variable for each numeric vector that splits it to intervals.
I know how to do it with one variable, but with multiple var names I get "Error: The LHS of :=
can't be spliced with !!!
".
Here is a reproducible example:
library(tidyverse)
vars <- c("wt", "qsec")
new_vars <- map_chr(vars, ~ paste0(.x, "_bin"))
bins <- map(vars, ~ quantile(mtcars[,.x], na.rm = T))
result <- mtcars %>%
mutate(!!!new_vars := map2(vars, bins, ~ cut(mtcars[,.x], breaks = .y)))
What I want to get is mtcars but with 2 extra columns. 1. wt_bin - which will be the quantile interval "wt" matches to 2. qsec_bin - which will be the quantile interval "qsec" matches to
I know it has something to do with !!!new_vars :=
but I can't seem to understand exactly the problem.
Thanks!