[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)
  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
________________________________________
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
https://stat.ethz.ch/mailman/listinfo/r-help
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