Re: [R] Case statements in R

From: Henrik Bengtsson <hb_at_stat.berkeley.edu>
Date: Mon, 28 Jul 2008 12:09:49 -0700

Hi.

On Mon, Jul 28, 2008 at 11:21 AM, Ted Harding <Ted.Harding_at_manchester.ac.uk> wrote:
> On 28-Jul-08 17:52:31, Henrik Bengtsson wrote:
>> Use '&' for vectors and '&&' for scalars. Ditto applies to the OR
>> operator(s). /Henrik
>
> What's wrong with using "&" for scalars? Surely it gives the
> correct answer? Maybe it's simply a bit slower, or something?

I don't know the details, but help("&&") says: "The operators '!', '&' and '|' are generic functions[...]". So, yes, you pay the unnecessary cost for dispatching, which you want to avoid for such basic operations. Moreover, personally I would consider it good programming practice to use '&&' when you really mean to. I'd rather see if (eventA && eventB) { ... } than if (eventA & eventB) { ... } even if they two objects are scalars. The latter one tells me that the developer might not know what s/he's doing. I don't if 'codeTools' already now picks on the latter, but it is not hard to imagine that it will one day.

My $0.02

Henrik

>
> Ted.
>
>> On Mon, Jul 28, 2008 at 10:36 AM, Wade Wall <wade.wall@gmail.com>
>> wrote:
>>> Hi all,
>>>
>>> I am trying to convert geometric means in a matrix to cover classes.
>>> My
>>> values are as such:
>>>
>>> perc<-c(0,0.025136418, 0.316227766, 1.414213562,3.16227766,
>>> 7.071067812,
>>> 15.8113883, 35.35533906, 61.23724357, 84.40971508, 97.46794345)
>>> cover<-c(0,1,2,3,4,5,6,7,8,9,10)
>>>
>>> This is what I am trying to accomplish
>>>
>>> veg_mean[veg_mean>0 && veg_mean < .1] <- 1
>>> veg_mean[veg_mean>= .1 && veg_mean < 1.0] <- 2
>>> veg_mean[veg_mean>=1.0 && veg_mean < 2.0] <- 3
>>> veg_mean[veg_mean>=2.0 && veg_mean < 5.0] <- 4
>>> veg_mean[veg_mean>= 5.0 && veg_mean < 10.0] <- 5
>>> veg_mean[veg_mean>= 10.0 && veg_mean < 25] <- 6
>>> veg_mean[veg_mean>= 25.0 && veg_mean < 50.0] <- 7
>>> veg_mean[veg_mean>=50.0 && veg_mean < 75.0] <- 8
>>> veg_mean[veg_mean>= 75.0 && veg_mean < 95.0 ] <- 9
>>> veg_mean[veg_mean>= 95.0 && veg_mean <= 100] <- 10
>>> veg_mean[veg_mean> 100] <- NA
>>>
>>> where values are assigned based on the geometric means. However, I
>>> think
>>> that my syntax for the && operator is wrong and I can't find a
>>> reference to
>>> proper syntax. I basically want to "bin" the geometric means.
>>>
>>> Any help would be greatly appreciated.
>>>
>>> Thanks,
>>>
>>> Wade
>>>
>>> [[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.
>
> --------------------------------------------------------------------
> E-Mail: (Ted Harding) <Ted.Harding_at_manchester.ac.uk>
> Fax-to-email: +44 (0)870 094 0861
> Date: 28-Jul-08 Time: 19:21:08
> ------------------------------ XFMail ------------------------------
>
> ______________________________________________
> 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 Mon 28 Jul 2008 - 19:22:03 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 Jul 2008 - 20:34:06 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