[R] Substitute and quotes

Thomas Lumley tlumley at u.washington.edu
Tue May 16 19:44:33 CEST 2006


On Tue, 16 May 2006, Kerpel, John wrote:
> I'm trying to update a model using:
>
> form <- substitute(.~.-bad.var, list(bad.var=bad.var))
> model.update<-update(model, form)
>
> where bad.var is a character string like lag(X1, 10)
>
> The problem is that substitute puts lag(X1, 10) in quotes - something I
> don't want it to do.

This is just a symptom. The problem is that lag(X1,10) is a character 
string, and you want a parsed expression

The solution depends on why lag(X1,10) is a string.  If you have
   bad.var <- "lag(X1,10)"
in your code you can replace it by
   good.var <- quote(lag(X1,10))

If "lag(X1,10)" comes in as a string in input then you need to parse it
    good.var <- parse(text=bad.var)


Also, the call to substitute() returns an unevaluated call rather than a 
formula, so you may need to eval() it before passing it to update().

 	-thomas




More information about the R-help mailing list