[R] Q: selecting a name when it is known as a string
Greg Snow
Greg.Snow at intermountainmail.org
Wed Sep 5 20:44:21 CEST 2007
If your main goal is to do a loess fit, then make predictions from that,
then using the 'get' function may do what you want:
tmp.var <- get(ORDINATE)
lo <- loess(percent ~ ncms * tmp.var, d, ...
grid <- expand.grid(tmp.var=MINVAL:MAXVAL, ncms=MINCMS:MAXCMS)
predict(lo, grid)
Here you stick with the name tmp.var so it matches between the formula
and the data frame, the predictions will be what you want. If you also
want to print out some of the model summary then realize that it will
have tmp.var in place of ds or whatever (It may not be too much trouble
to change that name after the fact if that is what is important).
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of D. R. Evans
> Sent: Tuesday, September 04, 2007 4:15 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Q: selecting a name when it is known as a string
>
> I am 100% certain that there is an easy way to do this, but
> after experimenting off and on for a couple of days, and
> searching everywhere I could think of, I haven't been able to
> find the trick.
>
> I have this piece of code:
>
> ...
> attach(d)
>
> if (ORDINATE == 'ds')
> { lo <- loess(percent ~ ncms * ds, d,
> control=loess.control(trace.hat =
> 'approximate'))
> grid <- data.frame(expand.grid(ds=MINVAL:MAXVAL,
> ncms=MINCMS:MAXCMS)) ...
>
> then there several almost-identical "if" statements for
> different values of ORDINATE. For example, the next "if"
> statement starts with:
>
> ...
> if (ORDINATE == 'dsl')
> { lo <- loess(percent ~ ncms * dsl, d,
> control=loess.control(trace.hat =
> 'approximate'))
> grid <- data.frame(expand.grid(dsl=MINVAL:MAXVAL,
> ncms=MINCMS:MAXCMS)) ...
>
> This is obviously pretty silly code (although of course it does work).
>
> I imagine that my question is obvious: given that I have a
> variable, ORDINATE, whose value is a string, how do I
> re-write statements such as the "lo <-" and "grid <-"
> statements above so that they use ORDINATE instead of the
> hard-coded names "ds" and "dsl".
>
> I am almost sure (almost) that it has something to do with
> "deparse()", but I couldn't find the right incantation, and
> the ?deparse() help left my head swimming.
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list