wrapr introduces an operator called “named map builder” that is written as “:=”. Named map builder is a very simple bit of code that performs a very simple task: it adds names to vectors or lists (making them work more like maps).
Here are some examples:
library("wrapr")
'a' := 5## a
## 5
c('a' := 5, 'b' := 6)## a b
## 5 6
c('a', 'b') := c(5, 6)## a b
## 5 6
The left-side argument of the := operator is called “the names”, and the right-side argument is called “the values”. The := operators returns the values with the names set to names.
A key use of the named map builder is the following:
key = 'keycode'
key := 'value'## keycode
## "value"
Notice the value inside the variable key was used as the array name, this differs from what is easily done with R’s native c(key = 'value') style notation.
A great use of the := operator is using it to conveniently build arguments lists for functions such as seplyr::mutate_se(). This works for simple explicit code such as the following.
library("seplyr")
datasets::iris %.>%
summarize_se(., "Mean_Sepal_Length" := "mean(Sepal.Length)")## Mean_Sepal_Length
## 1 5.843333
Slightly more complicated code such as:
datasets::iris %.>%
group_by_se(., "Species") %.>%
summarize_se(., c("Mean_Sepal_Length" := "mean(Sepal.Length)",
"Mean_Sepal_Width" := "mean(Sepal.Width)"))## # A tibble: 3 x 3
## Species Mean_Sepal_Length Mean_Sepal_Width
## <fct> <dbl> <dbl>
## 1 setosa 5.01 3.43
## 2 versicolor 5.94 2.77
## 3 virginica 6.59 2.97
Or even parametric code such as:
resultColumn <- "summary_value"
datasets::iris %.>%
group_by_se(., "Species") %.>%
summarize_se(., resultColumn := "mean(Sepal.Length)")## # A tibble: 3 x 2
## Species summary_value
## <fct> <dbl>
## 1 setosa 5.01
## 2 versicolor 5.94
## 3 virginica 6.59
For more details please see: help(:=, package = 'wrapr') and help("%.>%", package="wrapr").