Title: | Mathematical Operations on R Formula |
---|---|
Description: | Perform mathematical operations on R formula (add, subtract, multiply, etc.) and substitute parts of formula. |
Authors: | Bill Denney [aut, cre] |
Maintainer: | Bill Denney <[email protected]> |
License: | GPL-3 |
Version: | 0.5.0 |
Built: | 2024-11-11 02:58:06 UTC |
Source: | https://github.com/billdenney/formulops |
Extract formula parts
get_lhs(x) get_rhs(x)
get_lhs(x) get_rhs(x)
x |
A formula (or something that can be coerced to a formula) to extract a part from |
The requested part of the formula as a name or call or NULL
if
it does not exist.
get_lhs
: Extract the left hand side (NULL for one-sided
formula).
get_rhs
: Extract the right hand side.
Convert a substituting_formula object into a regular formula.
## S3 method for class 'substituting_formula' formula(x, ...)
## S3 method for class 'substituting_formula' formula(x, ...)
x |
the substituting_formula object |
... |
Ignored |
A formula with values substituted.
Modify a formula by finding some part of it and replacing it with a new value.
modify_formula(formula, find, replace, add_parens = FALSE)
modify_formula(formula, find, replace, add_parens = FALSE)
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 |
add_parens |
Add parentheses if |
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.)
formula
modified
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)))
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)))
Perform a mathematical operation on two formula
op_formula(op, e1, e2) multiply_formula(e1, e2) divide_formula(e1, e2) add_formula(e1, e2) subtract_formula(e1, e2) ## S3 method for class 'formula' Ops(e1, e2) ## S3 method for class 'formula' Math(x, ...)
op_formula(op, e1, e2) multiply_formula(e1, e2) divide_formula(e1, e2) add_formula(e1, e2) subtract_formula(e1, e2) ## S3 method for class 'formula' Ops(e1, e2) ## S3 method for class 'formula' Math(x, ...)
op |
The operation to perform either as a name or something that can be coerced into a name. |
e1 , e2 , x
|
The formulae to operate on |
... |
Ignored. |
The method for combination depends if the two formula are one- or two-sided.
If both formula are one-sided, the right hand side (RHS) of both are added together with additional parentheses added, if parentheses appear to be needed. If both formula are two-sided, the left hand side (LHS) and RHS are separately added. If one formula is one-sided and the other is two-sided, the LHS is selected from the two-sided formula and the RHS follows rules as though two one-sided formula were added.
multiply_formula
Multiply two formula (identical to (a~b) * (c~d)
divide_formula
Divide two formula (identical to (a~b) / (c~d)
add_formula
Add two formula (identical to (a~b) + (c~d)
subtract_formula
Multiply two formula (identical to (a~b) - (c~d)
Ops.formula
Supports generic binary operators and a couple of
unary operators (see ?Ops).
Math.formula
Supports generic unary operators (see ?Math).
e1
and e2
combined by the operation with the
environment from e1
. See Details.
op_formula("+", a~b, c~d) op_formula("+", a~b, ~d) op_formula("+", ~b, c~d) op_formula("+", ~b, ~d) op_formula("-", a~b) op_formula("-", -a~b) # Dumb, but accurate op_formula("-", -a~b, c~-d) # Dumb, but accurate log(a~b)
op_formula("+", a~b, c~d) op_formula("+", a~b, ~d) op_formula("+", ~b, c~d) op_formula("+", ~b, ~d) op_formula("-", a~b) op_formula("-", -a~b) # Dumb, but accurate op_formula("-", -a~b, c~-d) # Dumb, but accurate log(a~b)
Remove extraneous parentheses from a formula.
simplify_parens(x)
simplify_parens(x)
x |
The formula (or call) to simplify |
The simplified formula
simplify_parens(((a))~((b+c)))
simplify_parens(((a))~((b+c)))
A substituting formula helps clarify a formula where the parameters are more simply described in separate formulae.
substituting_formula(x, ...) as_substituting_formula(x, substitutions)
substituting_formula(x, ...) as_substituting_formula(x, substitutions)
x |
The base formula |
... |
Supporting formula of the form |
substitutions |
A list of supporting formula. |
Formula are substituted in order. Substitutions may not have the same left hand side.
A substituting_formula
object which may be coerced into a
single formula with an as.formula()
method or printed as a list of
formulae.
as_substituting_formula
: Generate and check substituting_formula
foo <- substituting_formula(y~x1+x2, x1~x3*x4, x2~x5/x6+x7) as.formula(foo)
foo <- substituting_formula(y~x1+x2, x1~x3*x4, x2~x5/x6+x7) as.formula(foo)