modify_formula: Modify a formula by finding some part of it and replacing it...

Description Usage Arguments Details Value Examples

View source: R/formula_math.R

Description

Modify a formula by finding some part of it and replacing it with a new value.

Usage

1

Arguments

formula

The formula to modify (may also be a call)

find

A call or name (or list thereof) to search for within the formula

replace

A call or name (or list thereof) to replace the find values

add_parens

Add parentheses if replace is not a name or if it is not already something in parentheses?

Details

Replacement occurs at the first match, so if the replacement list would modify something in the find list, that change will not occur (make two calls to the function for that effect). See the "Replacement is not sequential" examples below.

A special call can be used to expand a formula. If an expansion of arguments is desired to change a single function argument to multiple arguments, 'formulops_expand()' can be used. (See the examples.)

Value

formula modified

Examples

1
2
3
4
5
6
7
8
modify_formula(a~b, find=quote(a), replace=quote(c))
modify_formula(a~b, find=quote(a), replace=quote(c+d))
modify_formula(a~b/c, find=quote(b/c), replace=quote(d))
# Replacement is not sequential
modify_formula(a~b/c, find=list(quote(b/c), quote(d)), replace=list(quote(d), quote(e)))
modify_formula(a~b/c+d, find=list(quote(b/c), quote(d)), replace=list(quote(d), quote(e)))
# Expanding arguments to functions is possible
modify_formula(a~b(c), find=quote(c), replace=quote(formulops_expand(d, e)))

formulops documentation built on Feb. 22, 2020, 5:07 p.m.