[R] quick way to construct formula

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jun 29 09:05:42 CEST 2005


On Tue, 28 Jun 2005, Luke wrote:

> Dear R users,
>
> I have a data with 1000 variables named "x1", "x2", ..., "x1000", and
> I want to construct a formula like this format:
>
> ~x1+x2+...+x1000+x1:x2+x1:x3+x999:x1000+log(x1)+...+log(x1000)
>
> That is: the base variables followed by all interaction terms and all
> base feature log-transformations. I know I can use several paste
> functions to construct it. But is there any other handy way to do it?

Do you really want a formula with over half a million terms in?  I think 
it is likely that R will hit recursion limits in handing such a formula, 
quite possibly C-level stack overflow.

For a more modest example:

dd <- data.frame(x1=1, x2=2, x3=3)
terms(~ . + .^2, data=dd, simplify=TRUE)

will get you all terms and their interactions.  Using paste() to get the 
log terms is the easiest way I know.


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