Re: [R] Using if statement on function

From: Keith Jewell <k.jewell_at_campden.co.uk>
Date: Mon, 28 Jun 2010 11:45:37 +0100

You could also consider

    isTRUE(all.equal(FUN, mean))

> isTRUE(all.equal(mean, mean))
[1] TRUE
> isTRUE(all.equal(mean, median))
[1] FALSE HTH Keith J
"Patrick Burns" <pburns_at_pburns.seanet.com> wrote in message news:4C28777F.1040207_at_pburns.seanet.com...
> If I understand the problem properly,
> you want something like this:
>
> function(FUN, ...)
> {
> FunName <- deparse(substitute(FUN))
> if(FunName == "mean") {
> ...
> } else if(FunName == "median") {
> ...
> }
> }
>
> Using 'switch' is an alternative to 'if'.
>
>
> On 28/06/2010 10:50, Etienne Stockhausen wrote:
>> Hello everybody,
>>
>> I'm trying to use a if-statment on a function. For a better
>> understanding I want to present a small example:
>>
>> FUN=mean # could also be median,sd or any other function
>> if (FUN == mean)
>> plot(...)
>> if (FUN == median)
>> plot(...) ...
>>
>> This doesn't work, because FUN is a function. I've already tried to
>> coerce the type of FUN with as.character( ), but that's also not
>> possible. I'm stuck with this task and it is absolutely necessary to
>> give FUN the class of a function.
>> I'm looking forward for any hints, clues or solutions for my problem.
>>
>> So much thanks in advance
>>
>> Etienne
>>
>> ______________________________________________
>> 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.
>>
>
> --
> Patrick Burns
> pburns_at_pburns.seanet.com
> http://www.burns-stat.com
> (home of 'Some hints for the R beginner'
> and 'The R Inferno')
>



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. Received on Mon 28 Jun 2010 - 10:50:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 28 Jun 2010 - 12:50:42 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive