Re: [R] quick way to construct formula

From: Prof Brian Ripley <>
Date: Wed 29 Jun 2005 - 17:05:42 EST

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,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
PLEASE do read the posting guide!
Received on Wed Jun 29 18:12:14 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:06 EST