Hello my dear RStudio Community!
I'm having a bit of trouble with tidy evaluation in a mutate statement. The problem is the following:
I have a tibble:
library(tidyverse)
my_tibble <- tibble(
quantity = 10:20
)
my_tibble
# A tibble: 11 x 1
quantity
<int>
1 10
2 11
3 12
4 13
5 14
6 15
7 16
8 17
9 18
10 19
11 20
I want to create two summary variables: the mean of quantity
and a transformation of such mean. For example:
my_tibble %>%
summarise(
avg_3 = mean(quantity) / 3,
second_avg = sqrt(avg_3)
)
A tibble: 1 x 2
avg_3 second_avg
<dbl> <dbl>
1 5 2.24
Note that the second summary variable (second_avg
) depends on the first one (avg_3
).
The key here is that I want the "avg" string to be divided by a number (parameter) in this case, 3.
My goal is to create a function that does this with the following variables:
- The target tibble containing the data.
- The target variable on which to take the mean.
- The divisor by which the mean is divided (in my example, that's 3).
Recall that I want to name the first variable with the divisor parameter. For example, if divisor = 10
, I want the first variable to be named avg_10
. I can do that easily with tidy evaluation glue strings (https://www.tidyverse.org/blog/2020/02/glue-strings-and-tidy-eval/).
THE PROBLEM is getting the second summary function to work. Here's what I've tried so far:
average_and_divide <- function(.target_tbl = my_tibble,
.target_var = quantity,
.divisor = 3) {
avg_tbl <- .target_tbl %>%
summarise(
"avg_{{ .divisor }}" := mean({{ .target_var }}) / .divisor,
# here's the problem: I try to use the just-created variable but it won't work:
second_avg = sqrt("avg_{{ divisor }}") # here's the problem!!!
)
return(avg_tbl)
}
I know there should be a simple solution, but I couldn't find it. I've tried using enquo()
but that didn't work.