[R] Q: selecting a name when it is known as a string

Gustaf Rydevik gustaf.rydevik at gmail.com
Thu Sep 6 09:18:04 CEST 2007


On 9/6/07, Gustaf Rydevik <gustaf.rydevik at gmail.com> wrote:
> On 9/5/07, D. R. Evans <doc.evans at gmail.com> wrote:
> > D. R. Evans said the following at 09/04/2007 04:14 PM :
> > > I am 100% certain that there is an easy way to do this, but after
> >
> > I have reconsidered this and now believe it to be essentially impossible
> > (or at the very least remarkably difficult) although I don't understand why
> > it is so :-(

>
> How about:
>
> > foo<-c("a","b")
> > a<-1
> > b<-2
> > lapply(foo,function(x){eval(parse(text=paste(c("10+",x),collapse="")))})
> [[1]]
> [1] 11
>
> [[2]]
> [1] 12
>
>
> ,i.e you build up a text string containing your function call, and
> then evaluate it, once for each value of foo?
>
> /Gustaf

Applied to loess, it gives nice function names as well:

 a<-rnorm(10)
> b<-rnorm(10)
> lapply(foo,function(x){eval(parse(text=paste(c("loess(y~",x,")"),collapse="")))})
[[1]]
Call:
loess(formula = y ~ a)

Number of Observations: 10
Equivalent Number of Parameters: 4.61
Residual Standard Error: 1.219

[[2]]
Call:
loess(formula = y ~ b)

Number of Observations: 10
Equivalent Number of Parameters: 4.45
Residual Standard Error: 1.433


-- 
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik



More information about the R-help mailing list