Operators and Conditional statements
This is the fourth part of the series “R for Data Science”. The series is as follows:
- Part 1 – Introduction
- Part 2 – Basic Syntax
- Part 3 – Data Types and Objects
- Part 4 – Operators and Conditional Statements (this article)
- Part 5 – Loops and Control Statements
R Operators
R consists of different operators for performing different mathematical and logical operations.
Operators in R are mainly classified into the following categories:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
Arithmetic Operators
These operators are used for performing mathematical operations like addition, subtraction and multiplication etc. Following list shows the arithmetic operators can be used in R:
Operator |
Description |
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
^ | Exponent |
%% | Modulus |
%/% | Integer division |
Examples are:
>x <- 10 > y <- 15 #Addition > x+y [1] 25 #Subtraction > x-y [2] -5 #Multiplication > x*y [3] 150 #Division > y/x [4] 1.5 #Modulus > y%%x [5] 5 #Exponent > y^x [6] 576650390625 #Integer Division > y%/%x [7] 1
Relational Operators
Relational operators are used for comparing between values. Following list shows the relational operators can be used in R:
Operator | Description |
> | Greater than |
>= | Greater than or equal to |
< | Lesser than |
<= | Lesser than or equal to |
== | Exactly |
!= | Not Equal to |
Examples are:
> x <- 10 > y <- 15 #Greater Than > x>y [1] FALSE #Greater Than or Equal to > x>=10 [2] TRUE #Lesser than > x<y [3] TRUE #Lesser than or Equal to > y<=12 [4] FALSE #Exactly equal to > y==15 [5] TRUE
Logical Operators
Logical operators are used to perform Boolean operations like AND, OR etc.
Operator | Description |
! | Logical Not |
&& | Logical AND |
|| | Logical OR |
Examples are:
#|| Operator > c(TRUE, TRUE, FALSE) || c(TRUE, FALSE, FALSE) [1] TRUE #&& Operator > c(TRUE, 0, TRUE) && c(TRUE, FALSE, FALSE) [2] TRUE
Assignment Operators
These operators are used to assign values to variables.
Operator | Description |
<-,= | Leftwards Assignment |
-> | Rightwards Assignment |
Examples are:
#Leftwards Assignment > x <- 5 > x [1] 5 #Rightwards Assignment > 10 -> x > x [2] 10
Conditional Statements
Like other programming languages, R provides decision making features through conditional statements. Conditional Statements consist of:
- If statement
- If-else statement
- Nested if-else statement
If Statement
The if statement allows a block of code to be executed if and only if the condition provided in if statement is true. The syntax of if statement is:
if (condition) {
statement
}
Its example is:
p<-10 if(p>5){ print("Number is greater than 5") }
Output
[1] "Number is greater than 5"
If else statement
In this if the condition is TRUE, the statement within the if block gets executed and if the condition is FALSE, the statement in the else block is executed. It is necessary for the ‘else’ keyword to be in the same line where the closing brace of if statement is displayed. The syntax for this decision statement is:
if(condition) {
statement1
}
else{
statement2
}
The example for if else is:
x <--1 if(x >0){ print("Positive number") } else{ print("Negative number") }
Output
[1] "Negative number"
Nested if else statement
To use more than one condition, we use nested if else. Its syntax is:
if( condition1){
statement1
}
elseif(condition2){
statement2
}
elseif(condition3){
statement3
}
else
statement4
The nested if else can be easily understood with the following example:
x <-0 if(x <0){ print("Negative number") } elseif(x >0){ print("Positive number") } else print("Zero")
Output
[1] "Zero"
This is all for the lesson and now you can try these Operators and Condition Statements in R.
Please do comment if you have any query or doubt regarding the topic. Till then Happy Learning!
Trisha Singhal
Reach out if want to talk and work in the field of Data Science and Machine Learning. In addition to this, I'm a Third Year Undergraduate pursuing Bachelor's degree in Computer Science and Engineering.
Linkedin : https://www.linkedin.com/in/trishasinghal
Latest posts by Trisha Singhal (see all)
- The Proliferation of Data: Transforming Our Lives and Creating Breakthroughs - February 17, 2018
- R for Data Science – Part 4 – Operators and Conditional Statements - October 2, 2017
- R for Data Science – Part 1 – Introduction - August 31, 2017