-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnamed_map_builder.Rmd
More file actions
69 lines (48 loc) · 2 KB
/
named_map_builder.Rmd
File metadata and controls
69 lines (48 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
---
title: "Named Map Builder"
author: "John Mount"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Named Map Builder}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
[`wrapr`](https://github.com/WinVector/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:
```{r ex1}
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:
```{r key1}
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.
```{r ex2}
library("seplyr")
datasets::iris %.>%
summarize_se(., "Mean_Sepal_Length" := "mean(Sepal.Length)")
```
Slightly more complicated code such as:
```{r ex3}
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:
```{r ex4}
resultColumn <- "summary_value"
datasets::iris %.>%
group_by_se(., "Species") %.>%
summarize_se(., resultColumn := "mean(Sepal.Length)")
```
For more details please see: [`help(`:=`, package = 'wrapr')`](https://winvector.github.io/wrapr/reference/named_map_builder.html) and
[`help("%.>%", package="wrapr")`](https://winvector.github.io/wrapr/reference/dot_arrow.html).