Learn R Programming

cwhmisc (version 3.0)

str2formula: Extract sides of a formula to strings, Convert back to a formula

Description

str2formula is the inverse function to formula2string. term.names2formula combines two vectors of strings into a formula. formula2string Returns the left and the right hand sides of a formula. formula2term.names Returns one chosen side of a formula. formula2Rterm.names Returns the right hand side of a formula.

Usage

str2formula(s)
  term.names2formula(ls,rs)
  formula2string(form)
  formula2term.names(form,side)
  formula2Rterm.names(form)

Arguments

s
A list(left,right) containing the string representation of the left and the right hand side of the formula (one string each).
ls
A character vector (usually of length 1) containing the names of the terms on the left hand side of the formula.
rs
A character vector containing the names of the terms on the right hand side of the formula.
form
a formula.
side
one of "left","right".

Value

  • str2formula: A formula. formula2string: Character vector containing the string representation of the formulas side(s).

Examples

Run this code
fo <- a ~ b + c
abc <- formula2string(fo)  #  $left: "a"   $right: "b+c"
str2formula(abc)    # a ~ b + c
term.names2formula(formula2term.names(fo,"left"),c(formula2Rterm.names(fo),"X"))  # a ~ b + c + X
formula2string(a ~ b + c)  #  $left: "a"   $right: "b+c"
formula2string(~ b + c)    #  $left: ""    $right: "b+c"
formula2string(a ~ .)      #  $left: "a"   $right: "."
formula2term.names(a ~ b1 + c,"left")  #   "a"
formula2Rterm.names(a ~ b1 + c)        #   "b1" "c"

Run the code above in your browser using DataLab