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 em unimontes.br | chrysopa em gmail.com | http://www.ppgcb.unimontes.br/ | ICQ#: 5692561 | LinuxUser#: 205366
Hi, try this(Not tested): substitute(expression(1+2*pred+3*lat),list(pred=as.name(x1),lat=as.name(x2))) On 05/10/2007, Ronaldo Reis Junior <chrysopa@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@unimontes.br | > chrysopa@gmail.com > | http://www.ppgcb.unimontes.br/ | ICQ#: 5692561 | LinuxUser#: 205366 > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
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
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.
>
Ronaldo Reis Junior asked:> > I try to use a substitute function to generalise a equation. > > I have this: > > expression(1+2*pred+3*lat) > > (...) My expected result is: > > expression(1 + 2 * x1 + 3 * x2) >Check message by Thomas Lumley on 2007-03-27 (command... RSiteSearch("Replacement in an expression - can't use parse()") ... will get it among others) Quoting and adapting: e <- expression(1+2*pred+3*lat) do.call("substitute", list(e[[1]], list(pred=quote(x1),lat=quote(x2)))) # 1 + 2 * x1 + 3 * x2 (parece m?gica, n?o ??) Alberto Monteiro