[R] trouble automating formula edits when log or * are present; update trouble

Bert Gunter gunter.berton at gene.com
Tue May 29 20:10:40 CEST 2012


Paul et. al:

I think Gabor's incantation qualifies as my desired alternative to
eval(parse())). It is, unfortunately, rather tricky, imo.

However, the objection you raise to the gsub(deparse()) solution is
easily overcome through the use of an appopriate regex: e.g.:

 gsub("\\<x\\>","log(x)","x+xc+cx")
[1] "log(x)+xc+cx"

See the ?regex documentation on \< and \> . I believe this allows this
simple approach to handle all cases -- am I wrong about this?

Incidentally, I need to note that I was **wrong** in my previous statement that
formula(lm(y!x1))  does not work. It works fine, as there is a formula
method for (g)lm . I should look before I leap.

-- Bert




On Tue, May 29, 2012 at 10:41 AM, Paul Johnson <pauljohn32 at gmail.com> wrote:
>>>
>>
>> Try substitute:
>>
>>> do.call("substitute", list(newFmla, setNames(list(as.name("x1c")), "x1")))
>> y ~ log(x1c) + x2 * x3
>>
> Damn. That's pretty. I'd say "setNames" a magic bullet too.
>
> Thanks very much.
>
> The approach suggested by Michael and Bert has the little shortcoming
> that grepping for "x1" also picks up similarly named variables like
> "x1new" and "x1old".  I've not yet found a similar downside with
> Gabor's method.
>
> pj
>>
>> --
>> Statistics & Software Consulting
>> GKX Group, GKX Associates Inc.
>> tel: 1-877-GKX-GROUP
>> email: ggrothendieck at gmail.com
>
>
>
> --
> Paul E. Johnson
> Professor, Political Science    Assoc. Director
> 1541 Lilac Lane, Room 504     Center for Research Methods
> University of Kansas               University of Kansas
> http://pj.freefaculty.org            http://quant.ku.edu
>
> ______________________________________________
> 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list