[R] updating formula inside function

Christian Bieli bieli at biomillaufen.ch
Wed Jan 11 22:37:55 CET 2006


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




More information about the R-help mailing list