From murdoch at stats.uwo.ca  Tue Aug 23 04:21:50 2005
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Mon, 22 Aug 2005 22:21:50 -0400
Subject: [Rd] Questions about calls and formulas
In-Reply-To: <430A613C.5020705@univie.ac.at>
References: <430A4629.8010506@univie.ac.at>	<40e66e0b05082214525e2140e8@mail.gmail.com>	<430A556E.4080203@univie.ac.at>	<971536df050822155550243049@mail.gmail.com>
	<430A613C.5020705@univie.ac.at>
Message-ID: <430A883E.5040005@stats.uwo.ca>

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



[Rd] Questions about calls and formulas
Duncan Murdoch