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))
lhs, rhs | list or vector of character strings containing summands for left and right hand sides
of a formula. |
---|---|
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 |
#> a + b ~ x + y + log(x * y) #> <environment: 0x7f88325535a0>#> response ~ x * y * log(x) #> <environment: 0x7f88325535a0>#> iGDP ~ iK + iL + iQp + iYear #> <environment: 0x7f88325535a0>#> iGDP ~ iK + iL + iQp + iYear #> <environment: 0x7f88325535a0>#> [1] TRUEsummands(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>#> Error in lhs(f): could not find function "lhs"#> Error in eval(expr, envir, enclos): object 'h' not found