[R] Updating a formula w a portion of another formula

Gabor Grothendieck ggrothendieck at myway.com
Thu Dec 30 05:20:11 CET 2004


Gabor Grothendieck <ggrothendieck <at> myway.com> writes:

: 
: Daniel Almirall <dalmiral <at> umich.edu> writes:
: 
: : 
: : R-list,
: : 
: : Suppose I have
: : 
: :  oldfmla <- y ~ x
: : 
: : I would like to update it to  y ~ x + z  which I know I can get using
: : 
: :  newfmla <- update(oldfmla, ~ . + z)
: : 
: : However, what if I have
: : 
: : fmlatmp <- ~ z
: : 
: : Can I combine oldfmla and fmlatmp to get  y ~ x + z  some how?
: : 
: : Clearly,
: : 
: :  newfmla <- update(oldfmla, ~ . + fmlatmp)
: : 
: : will not work.
: : 
: : Thanks in advance,
: : Danny
: 
: Assuming we have:
: 
: 	old <- y ~ x
: 	tmp <- ~ z
: 
: Then type in this:
: 
: 	template <- . ~ . + X
: 	template[[3]][[3]] <- tmp[[2]]
: 	update(old, template)
: 

One can alternately use substitute avoiding determination of
the precise indices of X.  Note that the eval is required
since substitute returns a call object and eval turns it
back into a formula (as per the Note near the bottom of
?substitute):

       tmp2 <- eval(substitute(. ~ . + X, list(X = tmp[[2]])))
       update(old, tmp2)




More information about the R-help mailing list