# [R] converting to a language object

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Sat Feb 8 08:17:04 CET 2003

```In this case a simler answer is that you want a formula, so use
as.formula:

> eq <- "y ~ x1 + x2 +x2000"
> as.formula(eq)
y ~ x1 + x2 + x2000

once the quotation marks are in the right place.

It is much simpler to use lm(y ~ ., data=foo) !

A caveat: you may hit some limits on expression size if you try to put
2000 variables in a formula, and almost certainly you will hit
computational limits if you try to do a regression on it, as you need
n >> p = 2000, and the key computation is (I think) O(np^2).

On Fri, 7 Feb 2003, Marc Schwartz wrote:

> Arnab mukherji wrote:
> > Hi,
> >
> > I have a query about the following:
> >
> > CONTEXT: if we define:
> >
> > eq <- y ~ x1 + x2
> >
> > then is.language(eq) returns true. this works perfectly with commands
> > such as lm(eq,data=my.data)
> >
> > THE PROBLEM: Now I have a big data set with about 2000 independent
> > variables. So I tried to automate this. I can pick off the names of
> > the variables and insert a plus in between them and get a string.
> > Thus I have
> >
> > eq<- y ~ " x1 + x2 + ... +x2000" or eq<-"y ~ " x1 + x2 + ... +x2000"
> >
> > from either case how can I typecast eq into a langugae object and get
> > the lm command to work ?
> >
> > Does anyone have any suggestions?
> >
> > thanks
> >
> > Arnab.
>
>
> Arnab,
>
> Once you have your full formula string constructed you can use:
>
> eq <- eval(parse(text = string))
>
> This will put the model formula into eq.
>
>
> For example try:
>
> eq <- eval(parse(text = "y ~ x1 + x2"))
>
>
>
> Hope that helps.
>
>
> Regards,
>
> Marc Schwartz
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help
>

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

```