I want to apply different functions to the same column in a tibble. These functions are stored in a character string. I used to do this with mutate_
and the .dots
argument like this:
library(dplyr)
myfuns <- c(f1 = "a^2", f2 = "exp(a)", f3 = "sqrt(a)")
tibble(a = 1:3) %>%
mutate_(.dots = myfuns)
This approach still works fine but mutate_
is deprecated. I tried to achieve the same result with mutate
and the rlang
package but did not get very far.
In my real example myfuns
contains about 200 functions so typing them one by one is not an option.
Thanks in advance.
A base alternative :
Created on 2019-07-08 by the reprex package (v0.3.0)
For simple equations that take a single input, it’s sufficient to supply the function itself, e.g.
Or, when using an equation rather than a simple function, via a formula:
When using equations that access more than a single variable, you need to use rlang quosures instead:
Here,
quo
creates a “quosure” — i.e. a quoted representation of your equation, same as your use of strings, except, unlike strings, this one is properly scoped, is directly usable by dplyr, and is conceptually cleaner: It is like any other R expression, except not yet evaluated. The difference is as follows:1 + 2
is an expression with value3
.quo(1 + 2)
is an unevaluated expression with value1 + 2
that evaluates to3
, but it needs to be explicitly evaluated. So how do we evaluated an unevaluated expression? Well …:Then
!!
(pronounced “bang bang”) unquotes the previously-quoted expression, i.e. evaluates it — inside the context ofmutate
. This is important, becauseSepal.Length
andSepal.Width
are only known inside themutate
call, not outside of it.In all the cases above, the expressions can be inside a list, too. The only difference is that for lists you need to use
!!!
instead of!!
:The
!!!
operation is known as “unquote-splice”. The idea is that it “splices” the list elements of its arguments into the parent call. That is, it seems to modify the call as if it contained the list elements verbatim as arguments (this only works in functions, such asmutate
, that support it, though).One way using
parse_expr
fromrlang
Convert your strings to expressions
then pass those expressions to regular
mutate
using quasiquotation:Note that this will also work with strings / expressions involving multiple columns.
you can try also a
purrr
approachsome data
You have only one column, so both approaches below will give you the same result.
You only have to modify your functions' list.