Modify components of a formula by adding / removing vars from the rhs or replacing the lhs.

f_modify_formula(
  f,
  rhs_remove = NULL,
  rhs_add = NULL,
  lhs_replace = NULL,
  negate = TRUE
)

Arguments

f

formula

rhs_remove

regex or character vector for dropping variables from the rhs

rhs_add

character vector for adding variables to rhs

lhs_replace

string to replace formula lhs if supplied

negate

should rhs_remove keep or remove the specified vars. Set to FALSE to keep

Value

formula

Examples


iris %>%
tidy_formula(target = Species, tidyselect::everything()) -> f

f
#> Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
#> <environment: 0x0000018e86213c88>

f %>%
  f_modify_formula(
rhs_remove = c("Petal.Width", "Sepal.Length"),
rhs_add = "Custom_Variable"
)
#> Species ~ Sepal.Width + Petal.Length + Custom_Variable
#> <environment: 0x0000018e863769a0>

f %>%
  f_modify_formula(
rhs_remove = "Petal",
lhs_replace = "Petal.Length"
)
#> Petal.Length ~ Sepal.Length + Sepal.Width
#> <environment: 0x0000018e864f2ed0>