Some functions to make it easier to build and minipulate formulas

build_formula(lhs, rhs, env = parent.frame(), op = "+")

keep_summands(formula, n, right = TRUE, env = environment(formula),
  op = "+")

summands(formula, n, right = TRUE, op = "+")

replace_summand(formula, n, term, right = TRUE, op = "+",
  env = environment(formula))

Arguments

lhs, rhs

list or vector of character strings containing summands for left and right hand sides of a formula. NULLs are ignored.

env

an environment to attach to the formula

op

The operator to use for splitting into terms.

formula

a formula

n

a vector of integer indices. Negatives can be used to deselect.

right

a logical indicating whether to operate on the right side (else the left) of the formula.

term

a character string containing a new term

Examples

a <- build_formula(c("a", "b"), c("x", "y", "log(x*y)")); a
#> a + b ~ x + y + log(x * y) #> <environment: 0x7f88325535a0>
b <- build_formula("response", list("x", "y", NULL, "log(x)"), op="*"); b
#> response ~ x * y * log(x) #> <environment: 0x7f88325535a0>
f <- build_formula( "iGDP", list("iK", "iL", "iQp", "iYear")); f
#> iGDP ~ iK + iL + iQp + iYear #> <environment: 0x7f88325535a0>
g <- build_formula( "iGDP", c("iK", "iL", "iQp", "iYear")); g
#> iGDP ~ iK + iL + iQp + iYear #> <environment: 0x7f88325535a0>
#> [1] TRUE
summands(a)
#> [1] "x" "y" "log(x * y)"
summands(a, right=FALSE)
#> [1] "a" "b"
summands(b, op="*")
#> [1] "x" "y" "log(x)"
keep_summands(f, 3)
#> iGDP ~ iQp #> <environment: 0x7f88325535a0>
keep_summands(f, -3)
#> iGDP ~ iK + iL + iYear #> <environment: 0x7f88325535a0>
keep_summands(f, 2:4)
#> iGDP ~ iL + iQp + iYear #> <environment: 0x7f88325535a0>
h <- replace_summand( f, 3, paste(summands(f, 3), lhs(f), sep="/")); h
#> Error in lhs(f): could not find function "lhs"
#> Error in eval(expr, envir, enclos): object 'h' not found