Reputation: 3374
I am looking for a tidy way of incorporating vectorised operations on columns using dplyr.
Basically, having a simple df as follows:
library(dplyr)
df <- data.frame("X" = runif(1:10),
"Y" = runif(1:10), "Z" = runif(1:10)) %>%
tbl_df()
I am now looking to apply the following vectorised formula:
Formula <- "X / Y + lag(Z)"
Of course the following won't work as it is looking for a column 'X / Y + lag(Z)':
df %>% mutate(Result := !!sym(Formula))
Can anyone suggest a simple way of applying a formula from a vector directly in a pipe on columns to achieve:
df %>% mutate(Result = X/Y+lag(Z))
Upvotes: 2
Views: 133
Reputation: 886938
With tidyverse
, parse_expr
can be used
library(dplyr)
df <- df %>%
mutate(Calc_Col = !! rlang::parse_expr(Formula))
and if we need to pass the column name as variable, use the :=
(as @Nick mentioned in the comments)
Name <- "Calc_Col"
df <- df %>%
mutate(!!Name := !!rlang::parse_expr(Formula))
Upvotes: 1
Reputation: 2018
Is this what you're looking for?
set.seed(1)
df <- data.frame("X" = runif(1:10),
"Y" = runif(1:10), "Z" = runif(1:10)) %>%
tbl_df()
Formula <- "X / Y + lag(Z)"
df <- df %>% mutate(Result = eval(parse(text = Formula)))
X Y Z Result
<dbl> <dbl> <dbl> <dbl>
1 0.153 0.0158 0.527 NA
2 0.322 0.231 0.327 1.93
3 0.479 0.0958 0.365 5.33
4 0.764 0.537 0.105 1.79
5 0.180 0.223 0.0243 0.913
6 0.178 0.538 0.975 0.355
7 0.869 0.820 0.845 2.03
8 0.356 0.263 0.0628 2.20
9 0.0399 0.710 0.968 0.119
10 0.863 0.422 0.825 3.02
parse
an unevaluated expression, then eval
uate it.
Upvotes: 1