But all our solutions involve some hard coding (the worst in this respect being my last ones).
To make the function general and get rid of this hard coding, this is nicer:
library(tidyverse)
calculate_exp <- function(dat, var, value) {
dat %>%
mutate(!!quo_name(value) := var^value)
}
df <- data_frame(a = 1:10)
calculate_exp(df, df$a, 2)
#> # A tibble: 10 x 2
#> a `2`
#> <int> <dbl>
#> 1 1 1
#> 2 2 4
#> 3 3 9
#> 4 4 16
#> 5 5 25
#> 6 6 36
#> 7 7 49
#> 8 8 64
#> 9 9 81
#> 10 10 100
Now the function works with any data frame, any variable of that data frame, and any value.