Dear R-users, I haven?t found rules for logical operators. I need to select data according the following rule: Condition A & (Condition B | Condition C) How should I write it?? Is Condition A & Condition B | Condition C correct or will R execute (Condition A & Condition B) | Condition C ? Thanks for your help. Delphine Fontaine Delphine Fontaine Statistician Statistics Department - Genexion SA ------------------------------------------------------------ 29, Quai du Mont-Blanc Genva, CH-1201 Switzerland ------------------------------------------------------------ Office: +41 22 704 32 44 Fax:??? +41 22 704 32 42 Email: Delphine.Fontaine at genexion.com
Use parentheses if you want to be sure, otherwise the help file for & and | says: See Syntax for the precedence of these operators: unlike many other languages (including S) the AND and OR operators do not have the same precedence (the AND operators are higher than the OR operators). hth, Ingmar On 17 Jul 2007, at 08:59, Delphine Fontaine wrote:> Dear R-users, > > I haven’t found rules for logical operators. I need to select data > according > the following rule: > Condition A & (Condition B | Condition C) How should I write it ? Is > Condition A & Condition B | Condition C correct or will R execute > (Condition > A & Condition B) | Condition C ? > > Thanks for your help. > > Delphine Fontaine > > > > > > > Delphine Fontaine > Statistician > Statistics Department - Genexion SA > ------------------------------------------------------------ > 29, Quai du Mont-Blanc > Genva, CH-1201 > Switzerland > ------------------------------------------------------------ > Office: +41 22 704 32 44 > Fax: +41 22 704 32 42 > Email: Delphine.Fontaine@genexion.com > > ______________________________________________ > R-help@stat.math.ethz.ch 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.Ingmar Visser Department of Psychology, University of Amsterdam Roetersstraat 15 1018 WB Amsterdam The Netherlands t: +31-20-5256735 [[alternative HTML version deleted]]
?"&" will show See Syntax for the precedence of these operators: unlike many other languages (including S) the AND and OR operators do not have the same precedence (the AND operators are higher than the OR operators). Ted. Delphine Fontaine wrote on 07/17/2007 04:59 PM:> Dear R-users, > > I haven?t found rules for logical operators. I need to select data according > the following rule: > Condition A & (Condition B | Condition C) How should I write it ? Is > Condition A & Condition B | Condition C correct or will R execute (Condition > A & Condition B) | Condition C ? > > Thanks for your help. > > Delphine Fontaine > > > > > > > Delphine Fontaine > Statistician > Statistics Department - Genexion SA > ------------------------------------------------------------ > 29, Quai du Mont-Blanc > Genva, CH-1201 > Switzerland > ------------------------------------------------------------ > Office: +41 22 704 32 44 > Fax: +41 22 704 32 42 > Email: Delphine.Fontaine at genexion.com > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Dr E.A. Catchpole Visiting Fellow Univ of New South Wales at ADFA, Canberra, Australia _ and University of Kent, Canterbury, England 'v' - www.pems.adfa.edu.au/~ecatchpole / \ - fax: +61 2 6268 8786 m m - ph: +61 2 6268 8895
On 17-Jul-07 06:59:11, Delphine Fontaine wrote:> Dear R-users, > > I haven't found rules for logical operators. I need to select data > according the following rule: > Condition A & (Condition B | Condition C) > How should I write it_? Is Condition A & Condition B | Condition C > correct or will R execute (Condition A & Condition B) | Condition C ? > > Thanks for your help. > > Delphine Fontaine?Syntax will tell you about the precedence for operators. In particular you will find: '!' negation '& &&' and '| ||' or indicating that "!" takes precedence over "&" and "&&", which take precedence over "|" and "||". With equal precedence evaluation is from left to right. This shows that if you write A & B | C then "A & B" will be evaluated first, so the expression is equivalent to (A & B) | C. In any case, there is nothing whatever wrong with using "(... )" for grouping. It is always accepted, it forces the precedence you want, and furthermore (most important) you yourself can see exactly what will happen and will be much less likely to make a mistake. So write your condition in your code *explicitly* as ConditionA & (ConditionB | ConditionC) You can see what's happening, and R will do exactly what you want. Best wishes Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 17-Jul-07 Time: 09:34:18 ------------------------------ XFMail ------------------------------
Seemingly Similar Threads
- problem when extacting columns of a data frame in a new data frame
- R validation
- extracting pvalue from ANOVA with repeated measures
- R and clinical studies
- R validation. If you know what you want, it's simple. If you don't know what you need to do, there are problems...(with any validation)