This is probably buried somewhere in the R help archives, but I've been unable to find it, so perhaps the keywords I use here will help bring the topic to the surface more easily for future users. I want to write my own modeling function (ultimately for some multidimensional windowing - but this question is on scripting basics). For purposes of figuring out my needs, lets just consider writing a function that takes a formula and a dataset, and outputs the mean of each variable in the formula model. So: MyFunction <- function(AFormula, ADataFrame) { ... }>MyFunction(Y ~ A + B, TheData)would give results something like: Mean of MyData$Y is 5.3 Mean of MyData$A is 3.4 Mean of MyData$B is 8.2 QUESTION 1: How do I determine the terms in the formula within the function? QUESTION 2: How do I use one of those terms to get to the individual columns, like MyData$Y ? Thanks! -Eric --- Eric Peterson Vegetation Ecologist Nevada Natural Heritage Program [[alternative HTML version deleted]]
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:> This is probably buried somewhere in the R help archives, but I've been > unable to find it, so perhaps the keywords I use here will help bring the > topic to the surface more easily for future users. > > I want to write my own modeling function (ultimately for some > multidimensional windowing - but this question is on scripting basics). For > purposes of figuring out my needs, lets just consider writing a function > that takes a formula and a dataset, and outputs the mean of each variable in > the formula model. So: > > MyFunction <- function(AFormula, ADataFrame) { > ... > } > > >MyFunction(Y ~ A + B, TheData) > > would give results something like: > > Mean of MyData$Y is 5.3 > Mean of MyData$A is 3.4 > Mean of MyData$B is 8.2 > > QUESTION 1: > How do I determine the terms in the formula within the function? > > QUESTION 2: > How do I use one of those terms to get to the individual columns, like > MyData$Y ? > > Thanks! > -Eric > --- > Eric Peterson > Vegetation Ecologist > Nevada Natural Heritage Program > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 > and provide commented, minimal, self-contained, reproducible code. >
On Fri, 6 Jul 2007, Eric Peterson wrote:> This is probably buried somewhere in the R help archives, but I've been > unable to find it, so perhaps the keywords I use here will help bring the > topic to the surface more easily for future users. > > I want to write my own modeling function (ultimately for some > multidimensional windowing - but this question is on scripting basics). For > purposes of figuring out my needs, lets just consider writing a function > that takes a formula and a dataset, and outputs the mean of each variable in > the formula model. So: > > MyFunction <- function(AFormula, ADataFrame) { > ... > } > >> MyFunction(Y ~ A + B, TheData) > > would give results something like: > > Mean of MyData$Y is 5.3 > Mean of MyData$A is 3.4 > Mean of MyData$B is 8.2 > > QUESTION 1: > How do I determine the terms in the formula within the function?The function you seem to be missing is called terms(), strangely enough.> QUESTION 2: > How do I use one of those terms to get to the individual columns, like > MyData$Y ?Use model.frame to collect the data you want. What MyFunction(Y ~ A + B, TheData) conventionally means is to find Y, A, B, looking first in TheData (not MyData), then in the environment of the formula and then in the search path (see ?model.frame and http://developer.r-project.org/nonstandard-eval.pdf). See also http://developer.r-project.org/model-fitting-functions.txt.> Eric Peterson > Vegetation Ecologist > Nevada Natural Heritage Program-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595