[R] question on formula and terms.formula()

William Dunlap wdunlap at tibco.com
Wed Jul 13 18:21:10 CEST 2011

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)
  list(y, x1, Error(x2))
            x1 Error(x2)
  y          0         0
  x1         1         0
  Error(x2)  0         1
  [1] "x1"        "Error(x2)"
  [1] 3
  [1] 1 1
  [1] 1
  [1] 1
  <environment: R_GlobalEnv>

Bill Dunlap
TIBCO Spotfire
From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] on behalf of pangdu at vt.edu [pangdu at vt.edu]
Sent: Tuesday, July 12, 2011 8:40 PM
To: r-help at r-project.org
Subject: [R] question on formula and terms.formula()

I'm trying to create a formula object to pass on to a function that
applies the function terms.formula() to it.

f <- function(formula, ...)
mf <- match.call()
term <- terms.formula(mf$formula)

However, my code below gives an error.

form <- as.formula("y~x")
f(form, ...)

The error message was:
Error in terms.formula(mf$formula): argument is not a valid model.

Could anybody help me figure out the problem and find a solution here?

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list