Skip to content

An R package for using tidyselect-style selection helpers to build formula objects.

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

damian-t-p/tidyformula

Repository files navigation

tidyformula

R-CMD-check codecov CRAN status

tidyformula() translates formulas containing tidyselect-style selection helpers, expanding these helpers by evaluating dplyr::select() with the relevant selection helper on a supplied data frame.

Installation

You can install the development version of tidyformula from GitHub with:

# install.packages("devtools")
devtools::install_github("damian-t-p/tidyformula")

Example

We demonstrate how to build formulas from the variables of the following toy data frame:

library(tidyformula)

df <- data.frame(
   x1 = rnorm(5),
   x2 = rnorm(5),
   x3 = rnorm(5),
   y  = rnorm(5)
 ) 

The simplest usage is adding a selection of variables. The tidy-selected variables can be combined with other variables in the formula:

tidyformula(y ~ starts_with("x") + z, data = df)
#> y ~ x1 + x2 + x3 + z
#> <environment: 0x00000244c1c06720>

The selection helper can have additional arguments, as with num_range

tidyformula(y ~ num_range("x", 1:2) + z, data = df)
#> y ~ x1 + x2 + z
#> <environment: 0x00000244c1c06720>

When the selection helper appears as the first argument of a function, that function is distributed across the sum of the selected variables.

This works with single-argument functions

tidyformula(y ~ log(contains("x")), data = df)
#> y ~ log(x1) + log(x2) + log(x3)
#> <environment: 0x00000244c1c06720>

as well as multiple-argument ones.

tidyformula(y ~ poly(contains("x"), 3), data = df)
#> y ~ poly(x1, 3) + poly(x2, 3) + poly(x3, 3)
#> <environment: 0x00000244c1c06720>

The functions +, :, *, and ^ are not distributed by default.

tidyformula( ~ everything()*z + starts_with("x")^2, data = df)
#> ~(x1 + x2 + x3 + y) * z + (x1 + x2 + x3)^2
#> <environment: 0x00000244c1c06720>

This behaviour can be overwritten with the nodistribute argument, which is a character vector of functions that should not be distributed.

tidyformula( ~ everything()*z + starts_with("x")^2,
            data         = df,
            nodistribute = c("+", "-"))
#> ~x1 * z + x2 * z + x3 * z + y * z + (x1^2 + x2^2 + x3^2)
#> <environment: 0x00000244c1c06720>

About

An R package for using tidyselect-style selection helpers to build formula objects.

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Packages

No packages published

Languages