I would like to pass a data frame and its columns for processing dplyr mutate inside a function.
Here is an example
multifun <- function(dataf,vari){ mutate(dataf,newvar=vari*2) } multifun(mtcars,gear)
The problem with this function is that the variable 'gear' is not a recognized object. More specifically, I get an error
Error in mutate_impl object (.data, named_dots (...), environment ()) 'gear' not found
This is a problem with the environment in which dplyr mutate looks for the appropriate variable.
I understand it
multifun(mtcars,mtcars$gear)
will give me the answer that I want, namely
mpg cyl disp hp drat wt qsec vs am gear carb newvar 1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 8 2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 8 3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 8
but I would like to see if there is a way to avoid having to refer to every variable used from the data frame in the function call.
I also know that removing a mutation from a function call works without a problem. Namely, mutate(mtcars,newvar=gear*2) does its job. However, I am trying to understand how dplyr mutate looks for the right variable in different environments when placed inside a function.
source share