# [R] Multiple if function

Bert Gunter bgunter.4567 at gmail.com
Wed Sep 16 02:41:40 CEST 2015

```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.

However, maybe plyr or dplyr or data.table can do better, and I would
appreciate any comments from cogniscenti with these packages.  Maybe
I've been foolish by making the criteria too general, in which case my
knuckles need to be rapped.

Cheers,
Bert

Bert Gunter

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
-- Clifford Stoll

On Tue, Sep 15, 2015 at 5:00 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Sep 15, 2015, at 4:46 PM, David L Carlson wrote:
>
>> I realize that you can break this approach as well with a suitably complex expression, but I took it as a challenge:
>>
>>> dat <- data.frame(ASB = c(LETTERS[1:3]), Flow=c(11.51, 9.2, 10.5), stringsAsFactors=FALSE)
>>> cat <- LETTERS[1:3]
>>> mult <- c("'*'(2," , "'+'(5,", "sqrt(")
>>> sapply(parse(text=paste0(mult[match(dat\$ASB, cat)], dat\$Flow, ")")), eval)
>> [1] 23.02000 14.20000  3.24037
>
> This seems a bit less tortured:
>
>> mapply(  function(x,y) switch(x, A=y*2, B=y+3, C=sqrt(y) ), dat\$ASB, dat\$Flow )
>        A        B        C
> 23.02000 12.20000  3.24037
>
> Best;
> The Other David
>
>>
>> David
>>
>> -----Original Message-----
>> From: Bert Gunter [mailto:bgunter.4567 at gmail.com]
>> Sent: Tuesday, September 15, 2015 4:43 PM
>> To: David L Carlson <dcarlson at tamu.edu>
>> Cc: Peter Alspach <Peter.Alspach at plantandfood.co.nz>; r-help at r-project.org
>> Subject: Re: [R] Multiple if function
>>
>> Thanks, David.
>>
>> I would say, not quite.
>>
>> What if the alternatives are:
>>
>> If class = "a" multiply y by 2;
>> If class = "b"  add 5 to y;
>> If class = "c" take sqrt(y)
>> (where y is numeric, say)
>> ?
>>
>> Cheers,
>> Bert
>> Bert Gunter
>>
>> "Data is not information. Information is not knowledge. And knowledge
>> is certainly not wisdom."
>>   -- Clifford Stoll
>>
>>
>> On Tue, Sep 15, 2015 at 2:11 PM, David L Carlson <dcarlson at tamu.edu> wrote:
>>> You could use match() and avoid ifelse():
>>>
>>>> dat <- data.frame(ASB = c(LETTERS[1:3]), Flow=c(11.51, 9.2, 10.5), stringsAsFactors=FALSE)
>>>> cat <- LETTERS[1:3]
>>>> mult <- c(.1, .15, .2)
>>>> dat\$Flow * mult[match(dat\$ASB, cat)]
>>> [1] 1.151 1.380 2.100
>>>
>>> -------------------------------------
>>> David L Carlson
>>> Department of Anthropology
>>> Texas A&M University
>>> College Station, TX 77840-4352
>>>
>>>
>>> -----Original Message-----
>>> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Bert Gunter
>>> Sent: Tuesday, September 15, 2015 3:51 PM
>>> To: Peter Alspach
>>> Cc: r-help at r-project.org
>>> Subject: Re: [R] Multiple if function
>>>
>>> ... but this only works if ASBclass is numeric. What if it is a factor
>>> (or even character)?
>>>
>>> One can always finesse factors in simple cases like this, but for 2
>>> reasons, I don't think it's a good idea.
>>>
>>> 1. One should make use of a factor's API, rather than its internal
>>> integer representation(which, I grant, ain't likely to change);
>>>
>>> 2. For more complicated alternatives (e.g. entirely different
>>> functions depending on the factor value) it won't work anyway.
>>>
>>> For simple cases, ifelse() seems reasonable; but for more alternatives
>>> -- say 10 or 50 -- this becomes too cumbersome (imho). I think the
>>> split and recombine approach then becomes the best option, but maybe
>>> there is some easier, shorter, approach that I am overlooking. Please
>>> correct me if this is the case.
>>>
>>> Best,
>>> Bert
>>>
>>>
>>>
>>>
>>>
>>>
>>> Bert Gunter
>>>
>>> "Data is not information. Information is not knowledge. And knowledge
>>> is certainly not wisdom."
>>>   -- Clifford Stoll
>>>
>>>
>>> On Tue, Sep 15, 2015 at 12:23 PM, Peter Alspach
>>> <Peter.Alspach at plantandfood.co.nz> wrote:
>>>> Tena koe Maria
>>>>
>>>> It seems you need to multiply Flow by 0.05+ASBClass/20 (i.e., no if calls are necessary)
>>>>
>>>> HTH ....
>>>>
>>>> Peter Alspach
>>>>
>>>> -----Original Message-----
>>>> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Maria Lathouri
>>>> Sent: Tuesday, 15 September 2015 10:57 p.m.
>>>> To: r-help at r-project.org
>>>> Subject: [R] Multiple if function
>>>>
>>>> Dear all,
>>>>
>>>> I am writing as I would like your help. I have a dataframe with two columns, ASB and Flow, where the the first one has values 1, 2 or 3 and the second flow data. Something like that:
>>>> ASBclass    Flow1              11.51               9.2
>>>> 2              10.5
>>>> 3               6.7  ...              ...
>>>> I would like to produce a third column named eg. deviation where it would get me values based on if ASBclass is 1, multiply Flow by 0.1; if ASBclass is 2 then multiply Flow by 0.15 and if ASBclass is 3 then multiply by 0.2.
>>>>
>>>> If (ASBclass=1) { deviation<-Flow*0.1}
>>>> If (ASBclass=2) { deviation<-Flow*0.15}If (ASBclass=1) { deviation<-Flow*0.2} I am not sure whether I should add the else function and how can I combine these separate functions.
>>>>
>>>> Can anyone help me on that?
>>>> Thank you very much.
>>>>
>>>> Kind regardsMaria
>>>>
>>>>        [[alternative HTML version deleted]]
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>> The contents of this e-mail are confidential and may be subject to legal privilege.
>>>> If you are not the intended recipient you must not use, disseminate, distribute or
>>>> reproduce all or any part of this e-mail or attachments.  If you have received this
>>>> e-mail in error, please notify the sender and delete all material pertaining to this
>>>> e-mail.  Any opinion or views expressed in this e-mail are those of the individual
>>>> sender and may not represent those of The New Zealand Institute for Plant and
>>>> Food Research Limited.
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> and provide commented, minimal, self-contained, reproducible code.
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help