Skip to content
Prev 265503 / 398503 Next

question on formula and terms.formula()

I think you should replace
   terms.formula(formula)
by
   terms(formula)
When terms() is given a formula object it will
execute terms.formula but for other classes of
inputs it will invoke the appropriate method.
E.g., your formula may already be a terms object,
in which case terms.formula(formula) will waste
time reanalyzing it while terms(formula) will
invoke terms.terms, which just returns its input.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

-----Original Message-----
From: Pang Du [mailto:pangdu at vt.edu] 
Sent: Thursday, July 14, 2011 8:36 AM
To: William Dunlap; r-help at r-project.org
Subject: RE: [R] question on formula and terms.formula()

Thank you so much for your suggestion, Bill.

The R program I try to modify needs match.call() for something else. But the
problem does seem to be caused by this statement as you suggested. Following
this clue, I find out that 

terms.formula(formula)

does essentially what I want for "terms.formula(mf$formula)".

Pang Du
Virginia Tech


-----Original Message-----
From: William Dunlap [mailto:wdunlap at tibco.com] 
Sent: Wednesday, July 13, 2011 12:21 PM
To: pangdu at vt.edu; r-help at r-project.org
Subject: RE: [R] question on formula and terms.formula()

Does your code work if you omit the match.call step?  terms()
generally doesn't need that.  Also, do not call terms.formula(formula):
call just terms(formula).  The terms method appropriate to the class
of the formula will be used.

  > f1 <- function(formula, ...) {
  +     terms(formula, ...)
  + }
  > form <- as.formula("y ~ x1 + Error(x2)")
  > f1(form, specials="Error")
  y ~ x1 + Error(x2)
  attr(,"variables")
  list(y, x1, Error(x2))
  attr(,"factors")
            x1 Error(x2)
  y          0         0
  x1         1         0
  Error(x2)  0         1
  attr(,"term.labels")
  [1] "x1"        "Error(x2)"
  attr(,"specials")
  attr(,"specials")$Error
  [1] 3
  attr(,"order")
  [1] 1 1
  attr(,"intercept")
  [1] 1
  attr(,"response")
  [1] 1
  attr(,".Environment")
  <environment: R_GlobalEnv>

Bill Dunlap
TIBCO Spotfire