Friday 14 September 2018

Basic calculations: Logical operators in R Languages

Logical Operators and Comparisons

The following table shows the operations and functions for logical comparisons (True or False).




Examples :

> x = 1 : 6       # Generates x=1,2,3,4,5,6
> (x > 2) & (x < 5)  # Checks whether the values are greater than 2 and less than 5

[1]  FALSE  FALSE  TRUE  TRUE  FALSE  FALSE

> x [(x > 2) & (x < 5)]  # Finds which values are greater than 2 and smaller than 5.
[1]   3  4



Logical Operators and Comparisons 



  • The shoter form performs element-wise comparisons in almost the same way as arithmetic operators.
  • The longer from evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined.
Example of   " The longer form evaluates left to right examining only the first element of each vector"
> x = 1 : 6           # Generates x = 1,2,3,4,5,6

> (x > 2)  && (x < 5) 
 [1]  FALSE

is equivalent to:

> (x[1] > 2) & (x[1] < 5)
  [1]  FALSE


Note that x[1] is only the first element in x.

> x[ (x > 2) && (x < 5) ] 
   integer(0)                # Finds which values are greater than 2 and smaller than 5
This statement is equivalent to 

> x [ (x[1] > 2) & (x[1] < 5) ]
integer (0)


0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (89) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (743) Python Coding Challenge (195) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses