Learn R Programming

formula.tools (version 1.7.1)

split_terms: Split object into terms

Description

Split formulas, call and expressions into terms.

Usage

split_terms(x, recursive = FALSE)

Arguments

x

object to split terms from

recursive

logical; whether to split terms recursively in parenthetical enclosed terms (depth-first). (Default: FALSE)

Value

experession vector of terms

Details

Unlike the [terms()] function, `split_terms` does not use [stats::terms()] and instead just splits `x` into an **expression vector** of terms. (Terms are the mathematical notion of terms). The signs of the terms are preserved.

If `recursive` is `TRUE`, splitting occurs recursively, i.e. parsing of the input descends into parenthetical expressions `(...)`.

See **examples**.

References

* [SO: How to split a formula](https://stackoverflow.com/questions/39155701/how-to-split-a-formula-in-r)

See Also

* [terms()]

Examples

Run this code
# NOT RUN {
  
  split_terms(1)                   # 1
  split_terms( quote(a) )          # a     
  split_terms( quote(-a) )         # -a   
  split_terms( quote(a+1) )        # a, 1
  split_terms( quote(1+a) )        # 1, a
  split_terms( quote(-1+a) )       # -1, a
  split_terms( quote(-1-a) )
  
  split_terms( quote(a+b+c) )      # a,b,c
  split_terms( quote((a+b)+1) )    # (a+b),1
  split_terms( quote((a+b)+1), recursive=TRUE )    # a,b,1
  split_terms( quote((a-b)+1), recursive=TRUE )    # a,-b,1
  split_terms( quote(-a) )         # -a
  
  split_terms( quote(a-1) )        # a, -1
  split_terms( quote(-a-1))        # -a, -1
  split_terms( quote( -(a+1) ) )   # -(a+1)
  split_terms( quote( -(a+1) ), recursive=TRUE )  # -a,-1
  
  split_terms( quote( ---a ))
  split_terms( quote( -(a+(b-(c+d)))), recursive=TRUE )
  
# }

Run the code above in your browser using DataLab