[R] Multiple if function

David Winsemius dwinsemius at comcast.net
Wed Sep 16 23:10:24 CEST 2015

On Sep 15, 2015, at 7:20 PM, Charles C. Berry wrote:

> On Tue, 15 Sep 2015, Bert Gunter wrote:
>> Thanks to both Davids.
>> I realize that these things are often a matter of aesthetics -- and
>> hence have little rational justification -- but I agree with The Other
>> David: eval(parse) seems to me to violate R's soul( it makes R a macro
>> language instead of a functional one).
>> However, mapply(... switch) effectively loops through the frame row by
>> row. Aesthetically, I like it; but it seems inefficient. If there are
>> e.g. 1e6 rows in say 10 categories, I think Jeff's approach should do
>> much better.  I'll try to generate some actual data to see unless
>> someone else beats me to it.
> Use mapply like this on large problems:
> unsplit(
>    mapply(
>        function(x,z) eval( x, list( y=z )),
>        expression( A=y*2, B=y+3, C=sqrt(y) ),
>        split( dat$Flow, dat$ASB ),
>    dat$ASB)

Seems unnecessarily complex, but definitely elegant. Was there a reason it was not just:

       function(x,z) eval( x, list( y=z )),
       expression(A= y*2, B=y+3, C=sqrt(y) ),
       split( dat$Flow, dat$ASB )

Also readers should note that the names in that expression vector are quite arbitrary at the moment. The only association is via the order. I don't suppose someone wants to take on the challenge of matching the names of the expression vector with the names of returned split components?

> Chuck

David Winsemius
Alameda, CA, USA

More information about the R-help mailing list