Below are two dataframes, df and fmla. The fmla dataframe stores the variable name and the formula expression.
Using fmla dataframe, two calculated variables need to be added to df (purrr::map()?).
df <- tibble::tribble(
~a, ~b, ~c, ~d,
1000, 500, 300, 100,
1100, 560, 330, 110,
1200, 600, 360, 120,
1300, 650, 390, 130
)
fmla <- tibble::tribble(
~variable, ~formula,
"e", "(a+b)/c",
"f", "e*d"
)
If the calculated columns would have been manually added, the result would be similar to below calculation:
(result <- df %>%
mutate(e = (a+b)/c, f = e*d )
)
result <- tibble::tribble(
~a, ~b, ~c, ~d, ~e, ~f,
1000, 500, 300, 100, 5, 500,
1100, 560, 330, 110, 5.03, 553.33,
1200, 600, 360, 120, 5, 600,
1300, 650, 390, 130, 5, 650
)