Re: [R] looking for alternative of 'if-else'

From: <ctu_at_bigred.unl.edu>
Date: Sun, 06 Jul 2008 14:30:18 -0500

Hi,
I write the code for you. Hope this helps
> SD <- 1
> date1 <- seq(as.Date("2001-01-01"), as.Date("2002-12-1"), by = "day")
> len1 <- length(date1)
> set.seed(1) # to make it reproducible
> data1 <- zoo(rnorm(len1), date1)
> plot(data1)
>
> # calculation for monthly or quarterly mean
> calc = function(frequ = c("M.S", "Q.S")){

+        frequ<-match.arg(frequ)
+        switch(frequ,
+               M.S = aggregate(data1, as.yearmon, mean),
+               Q.S = aggregate(data1, as.yearqtr, mean))
+               }

>

Take a look what is the difference between my code and yours then you will know why your code does not work.

Chunhao Tu

Quoting Arun Kumar Saha <arun.kumar.saha_at_gmail.com>:

> I am not sure whether I could understand all things. Here is my code :
>
> library(zoo)
> # an reproducible example
> SD <- 1
> date1 <- seq(as.Date("2001-01-01"), as.Date("2002-12-1"), by = "day")
> len1 <- length(date1)
> set.seed(1) # to make it reproducible
> data1 <- zoo(rnorm(len1), date1)
> plot(data1)
>
> # calculation for monthly or quarterly mean
> calc = function(frequ = c("M S", "Q S"))
> {
> switch(frequ
> "M S" = aggregate(data1, as.yearmon, mean)
> "Q S" = aggregate(data1, as.yearqtr, mean))
> }
>
> However I am getting lot of errors while executing. Any further suggestion?
>
> Regards,
>
>
>
> On Sun, Jul 6, 2008 at 3:38 PM, <ctu_at_bigred.unl.edu> wrote:
>
>> Why don't you try "switch" Let me assume that you want to calculate a=3 and
>> b =5 by using one of "MEAN", "SUM","MIN", and "MAX functions. Then you could
>> write your code: (If you want to calculate "MEAN")
>>
>> example<-function(fun=c("MEAN", "SUM", "MIN", "MAX")){
>>>
>> + fun<-match.arg(fun);a=3;b=5
>> + switch( fun,
>> + MEAN=(a+b)/2,
>> + SUM=a+b,
>> + MIN=min(a,b),
>> + MAX=max(a,b))
>> + }
>>
>>> example("MEAN")
>>>
>> [1] 4
>>
>>>
>>> Hope this is helpful,
>> Chunhao Tu
>>
>>
>>
>>
>>
>>
>> Quoting Arun Kumar Saha <arun.kumar.saha_at_gmail.com>:
>>
>> There is "if-else" loop if I have to choose 1 item from a 2-item list.
>>> However if I have a list of 4 items (let say) then how i can choose a
>>> single
>>> item without employing 'if-else' loop? I mean in VBA I can use
>>> "select-case", is there any equivalent in R as well?
>>>
>>> Regards,
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> 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.
>>>
>>>
>>
>>
>>
>



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 Sun 06 Jul 2008 - 19:34:57 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 Sun 06 Jul 2008 - 20:33:27 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