Skip to content
Prev 119696 / 398498 Next

access to 'formula' terms in a user function

Try this:


MyFunction <- function(formula, data) {
	vars <- all.vars(formula)
	data.name <- deparse(substitute(data))
	for(v in vars)
		cat("Mean of ", data.name, "$", v, ": ",
			mean(data[[v]]), "\n", sep = "")
	invisible(colMeans(data[vars]))
}
MyFunction(~ Sepal.Length + Petal.Length, iris)
out <- MyFunction(~ Sepal.Length + Petal.Length, iris)
out
On 7/6/07, Eric Peterson <peterson at heritage.nv.gov> wrote: