(Note: This is not a direct answer to the original question. Since I don't have enough reputation to comment and I think the piece of information below is useful, I put it as an answer anyway. Please let me know if there is a better way to do so!)
Although you can't directly use :=
as =
or <-
, the :=
operator is very useful in programming with domain specific language (DSL) that use nonstandard evaluation (NSE), such as dplyr
and data.table
. Below is an example:
library(dplyr)
df <- tibble(
g1 = c(1, 1, 2, 2, 2),
g2 = c(1, 2, 1, 2, 1),
a = sample(5),
b = sample(5)
)
my_mutate <- function(df, expr) {
expr <- enquo(expr)
mean_name <- paste0("mean_", quo_name(expr))
sum_name <- paste0("sum_", quo_name(expr))
mutate(df,
!! mean_name := mean(!! expr),
!! sum_name := sum(!! expr)
)
}
my_mutate(df, a)
#> # A tibble: 5 x 6
#> g1 g2 a b mean_a sum_a
#> <dbl> <dbl> <int> <int> <dbl> <int>
#> 1 1. 1. 1 3 3. 15
#> 2 1. 2. 4 2 3. 15
#> 3 2. 1. 2 1 3. 15
#> 4 2. 2. 5 4 3. 15
#> # ... with 1 more row
In the example above, replacing :=
within the my_mutate
function with =
won't work, because !! mean_name = mean(!! expr)
isn't valid R code.
You can read more about NSE and programming with dplyr
here. It does a great job explaining how to handle NSE when using dplyr
functions to write your own function. My example above is directly copied from the website.
:=
is a very handy operator inside ofdata.table
, but not (yet?) in R as far as I know. – Pubes