[R] specifying dataframe column names in loops

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jul 4 20:50:32 CEST 2003


Sounds like you need to use substitute(). Something like

eval(substitute(lf <- locfit(~s, data=age), list(s=s)))

Formulae are different: I don't see why density() should be.

On Fri, 4 Jul 2003, Simon Goodman wrote:

> Dear list members,
> 
> This is probably a naive question (from a new user), but I'm having problems
> getting column 
> names to be recognised in loop that is supposed to apply a set of
> calculations in turn
> to each column of a dataframe.
> 
> The dataframe has 33 columns of output from a simulation.
> The columns each have a text name and I want to apply the same set of
> calculations
> to each column in turn. These include some self-defined functions that refer
> back to the 
> column in the current iteration.
> 
> Setting up a 'for' loop and using age[s] (where 'age' is the dataframe and
> 's' is the iterator) to specify
>  each column works for things like 'max' and 'min'. However for functions
> like 'density' or locfit, 
> error messages are generated:
> 
> lf<-locfit(~age[s])
> Error in model.frame(formula, rownames, variables, varnames, extras,
> extranames,  : 
>         invalid variable type

...

-- 
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




More information about the R-help mailing list