Passing in arguments into function
Hi Paul,
You need to pass the formula object, not a string. If you have a
function that is passing one of its arguments down to lm(), just pass
the argument directly, no need to do anything special. Here are some
examples using a built in dataset:
## wrapper function
foo <- function(fooform, ...) {
summary(lm(formula = fooform, ...))
}
## seeing it in action
foo(mpg ~ hp * wt, data = mtcars)
## save a formula in an object
myform <- mpg ~ hp * wt
## pass the object to foo() which passes it down
foo(myform, data = mtcars)
## pass the formula object "myform" directly to lm()
summary(lm(myform, data = mtcars))
Do one of those answer your question or do what you want?
Hope this helps,
Josh
On Sun, Jan 23, 2011 at 8:46 AM, Paul Evans <p.evans48 at yahoo.com> wrote:
Hi, I had a function that looked like: diff <- lm(x ~ y + z) How can I pass the argument to the 'lm' function on the fly? E.g., if I pass it in as a string (e.g. "x ~ y + z"), then the lm function treats it as a string and not a proper argument. many thanks ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/