[R] converting strings to expressions

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Sep 14 21:44:01 CEST 2006


"Deepayan Sarkar" <deepayan.sarkar at gmail.com> writes:

> Hi,
> 
> consider this:
> 
> --------------
> 
> estr <- c("2^4", "alpha[1]")
> eexp <- expression(2^4, alpha[1])
> 
> 
> ## Is it possible to get 'eexp' starting from 'estr'? The closest I could
> ## get was:
> 
> do.call(expression, lapply(estr, as.name))
> 
> ## but it is not quite the same; e.g. the following behave differently:

Er, how about

> estr <- c("2^4", "alpha[1]")
> parse(text=estr)
expression(2^4, alpha[1])

or (brain teaser alert!)

> parse(text=deparse(parse(text=estr)))[[1]]
expression(2^4, alpha[1])

which is _not_ quite the same thing.

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list