[R] converting to a language object
Marc Schwartz
MSchwartz at medanalytics.com
Sat Feb 8 16:41:02 CET 2003
ripley at stats.ox.ac.uk wrote:
> 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).
>
> SNIP
Valid points of course on all accounts.
Indeed there is a good example at the end of ?as.formula that addresses
the construction of a formula from a large number of variables with just
the type of sequencing that Arnab is using. To wit:
## Create a formula for a model with a large number of variables:
xnam <- paste("x", 1:25, sep="")
(fmla <- as.formula(paste("y ~ ", paste(xnam, collapse= "+"))))
which obviously results in:
y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 +
x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20 + x21 +
x22 + x23 + x24 + x25
I had been in the habit of using the more "generic" approach of
eval(parse(text = charvector)) when constructing R code for evaluation
in other situations.
Regards and thanks,
Marc Schwartz
More information about the R-help
mailing list