tidyformula {tidyformula} | R Documentation |
Build a formula using tidyselect
-style selection helpers
Description
tidyformula()
translates formulas containing tidyselect
-style
selection helpers, expanding these helpers by evaluating
dplyr::select()
with the relevant selection helper and a supplied data frame.
When the selection helper appears as the first argument of a function, that function is distributed across the sum of the selected variables.
Usage
tidyformula(
formula,
data,
select_helpers = .select_helpers,
nodistribute = c("+", "-", "*", "^"),
env = rlang::caller_env()
)
Arguments
formula |
An object of class |
data |
A data frame whose column names should be used for selection |
select_helpers |
A character vector. The names of selection helpers to be matched and substituted. |
nodistribute |
A character vector. Functions with these names are not distributed over selection helpers. |
env |
The environment to associate with the result. |
Value
An object of class formula
, which is a translation of the argument
formula
in which the selection helpers are replaced with the corresponding
variables of data
.
Examples
df <- data.frame( x1 = rnorm(5), x2 = rnorm(5), x3 = rnorm(5), y = rnorm(5) ) tidyformula(y ~ num_range("x", 1:2) + z, data = df) #> y ~ x1 + x2 + z #> <environment: 0x000001e0d7d53910> tidyformula(y ~ poly(starts_with("x"), 3), data = df) #> y ~ poly(x1, 3) + poly(x2, 3) + poly(x3, 3) #> <environment: 0x000001e0d7d53910> tidyformula( ~ everything() * contains("x"), data = df) #> ~(x1 + x2 + x3 + y) * (x1 + x2 + x3) #> <environment: 0x000001e0d7d53910>
Interaction operators are typically not distributed, but this behaviour can be changed.
tidyformula(y ~ starts_with("x")^2, data = df) #> y ~ (x1 + x2 + x3)^2 #> <environment: 0x000001e0d7d53910> tidyformula(y ~ starts_with("x")^2, data = df, nodistribute = c("+", "-")) #> y ~ x1^2 + x2^2 + x3^2 #> <environment: 0x000001e0d7d53910>
See Also
dplyr::select()
, tidyselect::language
for
documentation of selection helpers.