Vadim Ogranovich
2007-May-03 12:57 UTC
[R] convert text to exprission good for lm arguments
Hi, I ran into a problem of converting a text representation of an expression into parsed expression to be further evaluated inside lm ().> n <- 100 > data <- data.frame(x= rnorm (n), y= rnorm (n)) > data. lm <- lm (y ~ x, data=data) > > ## this works > update(data. lm , subset=x<0)Call: lm (formula = y ~ x, data = data, subset = x < 0) Coefficients: (Intercept) x -0.07864094193322170023 -0.14596982635007796358> > ## this doesn't work > ## text representation of subset > subset <- "x<0" > update(data. lm , subset=parse(text=subset))Error in `[.data.frame`(list(y = c(-0.601925958140825, -0.111931189071517, : invalid subscript type What is the correct way to convert "x<0" into a valid subset argument? Thanks, Vadim [[alternative HTML version deleted]]
Gabor Grothendieck
2007-May-03 13:11 UTC
[R] convert text to exprission good for lm arguments
Try:
do.call("update", list(data.lm, subset = parse(text = subset)))
On 5/3/07, Vadim Ogranovich <vogranovich at jumptrading.com>
wrote:> Hi,
>
> I ran into a problem of converting a text representation of an expression
into parsed expression to be further evaluated inside lm ().
>
> > n <- 100
> > data <- data.frame(x= rnorm (n), y= rnorm (n))
> > data. lm <- lm (y ~ x, data=data)
> >
> > ## this works
> > update(data. lm , subset=x<0)
>
> Call:
> lm (formula = y ~ x, data = data, subset = x < 0)
>
> Coefficients:
> (Intercept) x
> -0.07864094193322170023 -0.14596982635007796358
>
> >
> > ## this doesn't work
> > ## text representation of subset
> > subset <- "x<0"
> > update(data. lm , subset=parse(text=subset))
> Error in `[.data.frame`(list(y = c(-0.601925958140825, -0.111931189071517,
:
> invalid subscript type
>
> What is the correct way to convert "x<0" into a valid subset
argument?
>
> Thanks,
> Vadim
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Vladimir Eremeev
2007-May-03 13:16 UTC
[R] convert text to exprission good for lm arguments
Vadim Ogranovich wrote:> > Hi, > > I ran into a problem of converting a text representation of an expression > into parsed expression to be further evaluated inside lm (). > >> n <- 100 >> data <- data.frame(x= rnorm (n), y= rnorm (n)) >> data. lm <- lm (y ~ x, data=data) >> >> ## this works >> update(data. lm , subset=x<0) > > Call: > lm (formula = y ~ x, data = data, subset = x < 0) > > Coefficients: > (Intercept) x > -0.07864094193322170023 -0.14596982635007796358 > >> >> ## this doesn't work >> ## text representation of subset >> subset <- "x<0" >> update(data. lm , subset=parse(text=subset)) > Error in `[.data.frame`(list(y = c(-0.601925958140825, -0.111931189071517, > : > invalid subscript type > > What is the correct way to convert "x<0" into a valid subset argument? >update(data.lm,subset=eval(parse(text=subset))) -- View this message in context: http://www.nabble.com/convert-text-to-exprission-good-for-lm-arguments-tf3686309.html#a10304608 Sent from the R help mailing list archive at Nabble.com.