Re: [Rd] Questions about calls and formulas

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Tue 23 Aug 2005 - 02:21:50 GMT

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



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue Aug 23 12:25:02 2005

This archive was generated by hypermail 2.1.8 : Mon 24 Oct 2005 - 22:27:41 GMT