Named Map Builder

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

c('a' := 5, 'b' := 6)

c('a', 'b') := c(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'

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)")

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)"))

Or even parametric code such as:

resultColumn <- "summary_value"
datasets::iris %.>%
  group_by_se(., "Species") %.>%
  summarize_se(., resultColumn := "mean(Sepal.Length)")

For more details please see: help(:=, package = 'wrapr') and help("%.>%", package="wrapr").



Try the seplyr package in your browser

Any scripts or data that you put into this service are public.

seplyr documentation built on Sept. 5, 2021, 5:12 p.m.