Referring to dataframe column inside summarise_all, mutate_all, etc?

Is there a way to refer to other dataframe columns in a function that's called from summarise_all, mutate_all and similar?

Simple example:

summarise_all(mtcars, function(x) mean((x - mpg)/mpg))
# Error in (function (x)  : object 'mpg' not found

summarise_all(mtcars, function(x) mean((x - .$mpg)/.$mpg))
# Error in (function (x)  : object '.' not found

summarise_all(mtcars, function(x) mean((x - .x$mpg)/.x$mpg))
# Error in mean((x - .x$mpg)/.x$mpg) : object '.x' not found

This is with dplyr 0.8.3.

Hi @Hong. You can pass the argument like the following.

summarise_all(mtcars, function(x, mpg) mean((x - mpg)/mpg), mpg = mtcars$mpg)
4 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.