rules()
is a function designed to create input for .funs
argument of
scoped dplyr
"mutating" verbs (such as
summarise_all() and
transmute_all()). It converts bare expressions
with .
as input into formulas and repairs names of the output.
rules(..., .prefix = "._.")
Bare expression(s) with .
as input.
Prefix to be added to function names.
rules()
repairs names by the following algorithm:
Absent names are replaced with the 'rule__\ind\' where \ind\ is the
index of function position in the ...
.
.prefix
is added at the beginning of all names. The default is ._.
. It
is picked for its symbolism (it is the Morse code of letter 'R') and rare
occurrence in names. In those rare cases it can be manually changed but
this will not be tracked further. Note that it is a good idea for
.prefix
to be syntactic, as dplyr
will force tibble
names to be syntactic. To check if string is "good", use it as input to
make.names()
: if output equals that string than it is a "good" choice.
# `rules()` accepts bare expression calls with `.` as input, which is not
# possible with advised `list()` approach of `dplyr`
dplyr::summarise_all(mtcars[, 1:2], rules(sd, "sd", sd(.), ~ sd(.)))
#> mpg_._.rule__1 cyl_._.rule__1 mpg_._.rule__2 cyl_._.rule__2 mpg_._.rule__3
#> 1 6.026948 1.785922 6.026948 1.785922 6.026948
#> cyl_._.rule__3 mpg_._.rule__4 cyl_._.rule__4
#> 1 1.785922 6.026948 1.785922
dplyr::summarise_all(mtcars[, 1:2], rules(sd, .prefix = "a_a_"))
#> mpg_a_a_rule__1 cyl_a_a_rule__1
#> 1 6.026948 1.785922
# Use `...` in `summarise_all()` to supply extra arguments
dplyr::summarise_all(data.frame(x = c(1:2, NA)), rules(sd), na.rm = TRUE)
#> ._.rule__1
#> 1 0.7071068