[Rd] Questions about calls and formulas

Duncan Murdoch murdoch at stats.uwo.ca
Tue Aug 23 04:21:50 CEST 2005


Erich Neuwirth wrote:
>>Try do.call like this:
>>
>>ff <- x ~ g*h
>>do.call("substitute", list(ff, list(x = as.name("weight"))))
>>
> 
> 
> It is even more complicated.
> All I know is that ff is a formula with an expression on the left hand
> side. This expression needs to be replaced by "weights".
> According to the documentation, substitute only handles
> replacement of variables by something else,
> and that is not enough in my case.

That may make it simpler.  For example:

 > ff <- x ~ g*h
 > ff[[2]]
x
 > ff[[2]] <- as.name("weights")
 > ff
weights ~ g * h


If you know that ff is a formula with an expression on the left that you 
want to replace with "weights", then ff[[2]] <- as.name("weights")
will work.  (ff[[1]] is the tilde, ff[[3]] is the RHS.  You'll probably 
want some sanity checks in your code to confirm this.)

Duncan Murdoch



More information about the R-devel mailing list