Chapter 4 : Operators in R



Type of Operators

Operators in R can be majorly classified into four broader categories.

  1. Arithmetic Operators
  2. Assignment Operator
  3. Relational Operators
  4. Logical Operators

Let’s understand them one by one.

Arithmetic Operators

+ , - , * , / , %% , %/% , ^ , ** are all arithmetic operators in R.

+ Operator is used for addition
- Operator is used for subtraction
* Operator is used for multiplication
/ Operator is used for division
%% Operator is used to find the remainder
%/% Operator is used to find the quotient
^ or ** Operator is used for finding the power of a number.

Let’s see some examples where these operators are used.

Example 1: Adding two numbers 2 and 3 using + Arithmetic operator.

2 + 3
[1] 5

Example 2: Adding three numbers 12, 15 and 23 using + Arithmetic operator.

12 + 15 + 23
[1] 50

Example 3: Adding three numbers 225, 432 and 21 using + Arithmetic operator.

225 + 432 + 21
[1] 678

Example 4: Subtracting 2 from 3 using - Arithmetic operator.

3 - 2
[1] 1

Example 5: Subtracting 12 from 15 using - Arithmetic operator.

15 - 12 
[1] 3

Example 6: Multiplying 23 by 32

23 * 32
[1] 736

Example 7: Multiplying 223 by 192

223 * 192
[1] 42816

Example 8: Dividing 32 by 4

32 / 4
[1] 8

Example 9: Dividing 24364 by 4

24364 / 4
[1] 6091

Example 10: Finding remainder when we divide 12 by 5.

12 %% 5
[1] 2

Example 11: Finding remainder when we divide 231 by 2.

231 %% 2
[1] 1

Example 12: Finding quotient when we divide 12 by 5.

12 %/% 5
[1] 2

Example 13: Finding quotient when we divide 231 by 2.

231 %/% 2
[1] 115

Example 14: Finding 125.

12^5
[1] 248832

Alternate method,

12**5
[1] 248832

Example 15: Finding 223.

22^3
[1] 10648

Alternate method,

22**3
[1] 10648

Assignment Operator

<- is the assignment operator in R. It is actually equivalent to equal to sign =. In general, we say a = 2, which means we are assigning a value 2 to a variable a. In R, we write a <- 2 instead of a = 2.

Example 16: Create a variable b and assign a value 15 to it, using assignment operator.

b <- 15

Example 17: Create a variable c and assign a value -25 to it, using assignment operator.

c <- -25

Relational Operators

We use relational operators to check the relation between two or more variables. Below mentioned are some examples of relational operators which we use is R.

Example 18: Create two variables a=5 and b=3 and check the relationship using relational operators.

Assigning values to variable a and b as given in question.

a <- 5
b <- 3

Checking whether a is less than b or not.

a < b 
[1] FALSE

Checking whether a is greater than b or not.

a > b
[1] TRUE

Checking whether a is less or equal to b or not.

a <= b
[1] FALSE

Checking whether a is greater or equal to b or not.

a >= b
[1] TRUE

Checking whether a is equal to b or not.

a == b
[1] FALSE

Checking whether a is not equal to b or not.

a != b
[1] TRUE

Now, let’s see the summary of what we have done in the below snapshot.

Logical Operators

These operators are used to perform Boolean expression like AND and OR.

We use & symbol for AND and | symbol for OR.

Example 19: Create two variables a=5 and b=3 and check the below relationship using logical operators.

1. a==5&b==3
2. a==4&b==3
3.a==4&b==2
4. a==5&b==2
5. a==5|b==3
6. a==4|b==3
7.a==4|b==2
8. a==5|b==2

Assigning values to variable a and b as given in question.

a <- 5
b <- 3

Solution 1: Checking a==5&b==3

a==5 will return a value TRUE
b==3 will also return a value TRUE
We know, TRUE&TRUE is TRUE

a==5&b==3
[1] TRUE

Solution 2: Checking a==4&b==3

a==4 will return a value FALSE
b==3 will also return a value TRUE
We know, FALSE&TRUE is FALSE

a==4&b==3
[1] FALSE

Solution 3: Checking a==4&b==2

a==4 will return a value FALSE
b==2 will also return a value FALSE
We know, FALSE&FALSE is FALSE

a==4&b==2
[1] FALSE

Solution 4: Checking a==5&b==2

a==4 will return a value TRUE
b==2 will also return a value FALSE
We know, TRUE&FALSE is FALSE

a==5&b==2
[1] FALSE

Solution 5: Checking a==5|b==3

a==5 will return a value TRUE
b==3 will also return a value TRUE
We know, TRUE|TRUE is TRUE

a==5|b==3
[1] TRUE

Solution 6: Checking a==4&b==3

a==4 will return a value FALSE
b==3 will also return a value TRUE
We know, FALSE|TRUE is TRUE

a==4|b==3
[1] TRUE

Solution 7: Checking a==4&b==2

a==4 will return a value FALSE
b==2 will also return a value FALSE
We know, FALSE|FALSE is FALSE

a==4|b==2
[1] FALSE

Solution 8: Checking a==5|b==2

a==4 will return a value TRUE
b==2 will also return a value FALSE
We know, TRUE|FALSE is TRUE

a==5|b==2
[1] TRUE



<<Previous Next>>



© Copyright 2021
[RUHH ROBOTICS AUTHORS]
www.examsforwinners.com