[R] updating formula inside function

From: Christian Bieli <bieli_at_biomillaufen.ch>
Date: Thu 12 Jan 2006 - 08:37:55 EST


Dear R-Helpers

Given a function like
foo <- function(data,var1,var2,var3) {

	f <- formula(paste(var1,'~',paste(var2,var3,sep='+'),sep=''))
	linmod <- lm(f)
	return(linmod)

}

By typing
foo(mydata,'a','b','c')
I get the result of the linear model a~b+c. How can I rewrite the function so that the formula can be updated inside the function, i.e.
foo <- function(data,var1,var2,var3,var4) {
	f <- formula(paste(var1,'~',paste(var2,var3,sep='+'),sep=''))
	linmod <- lm(f)
	return(linmod)
	f2 <- update.formula(f,.~.-var3+var4)

}

Like that it won't work because var3 and var4 are characters, but also with substitute() and eval() I did not manage to get the favoured result. Can somebody help me out?
Thank you in advance

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jan 12 08:47:49 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:03 EST