I would like to pass a data frame and its columns to be processed by dplyr's mutate within 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 the error
Error in mutate_impl(.data, named_dots(...), environment()) object 'gear' not found
This is a problem with the environment where dplyr's mutate is looking for the variable in question.
I understand that
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 of avoiding the need to reference each variable used from the data frame in the function call.
I am also aware that taking mutate out of the function call works without problems. Namely, mutate(mtcars,newvar=gear*2)
does the job. However, I am trying to understand how dplyr's mutate is looking for the variable in question in the different environments when placed inside a function.