[R] help in substitute function

Gabor Grothendieck ggrothendieck at gmail.com
Fri Oct 5 16:07:20 CEST 2007


On 10/5/07, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> Given this:
>
> x1 <- "pred"
> x2 <- "lat"
> e <- expression(1+2*pred+3*lat)
>
> Try this:
>
> L <- list(as.name("x1"), as.name("x2"))
> names(L) <- c(x1, x2)
> do.call(substitute, list(e, L)) # e & L are evaluated first
>
> or perhaps:
>
> f <- e
> f <- sub("pred", "x1", f) # also converts to character
> f <- sub("lat", "x2", f)
> parse(text = sub("lat", "x2", f)) # convert back to expression

The last line was supposed to be just:

parse(text = f)


>
>
> On 10/5/07, Ronaldo Reis Junior <chrysopa at gmail.com> wrote:
> > Hi,
> >
> > I try to use a substitute function to generalise a equation.
> >
> > I have this:
> >
> > expression(1+2*pred+3*lat)
> >
> > I need to define the equation in function of x1 and x2 variables. Also I try
> > to define who is x1 and x2. I try this:
> >
> > X1 <- "pred"
> > X2 <- "lat"
> >
> > Now I need substitute pred and lat in equation by x1 and x2 defined on X1 and
> > X2 above, I try this way:
> >
> > >
> > substitute(expression(1+2*pred+3*lat),list(as.name(X1)=as.name(x1),as.name(X2)=as.name(x2))
> > Error: unexpected '='
> > in "substitute(expression(1+2*pred+3*lat),list(as.name(X1)="
> >
> > My expected result is:
> >
> > expression(1 + 2 * x1 + 3 * x2)
> >
> > Any help?
> > Thanks
> > Ronaldo
> > --
> > > Prof. Ronaldo Reis Júnior
> > |  .''`. UNIMONTES/Depto. Biologia Geral/Lab. de Biologia Computacional
> > | : :'  : Campus Universitário Prof. Darcy Ribeiro, Vila Mauricéia
> > | `. `'` CP: 126, CEP: 39401-089, Montes Claros - MG - Brasil
> > |   `- Fone: (38) 3229-8187 | ronaldo.reis at unimontes.br | chrysopa at gmail.com
> > | http://www.ppgcb.unimontes.br/ | ICQ#: 5692561 | LinuxUser#: 205366
> >
> > ______________________________________________
> > R-help at r-project.org 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.
> >
>



More information about the R-help mailing list