Operators in R Programming
R Programming offers various types of operators to perform the conventional operations like assignment, conditional checks, addition, subtraction, etc.
Types of Operators
- Assignment operator
- Arithmetic operator
- Relational operator
- Logical operator
- Special operator
Non available Operators, but implemented in an alternate approach
- Unary operator
- Bitwise operator
- Ternary operator
R Programming Assignment Operator
R Programming provides the Assignment operator in various flavors.
Name |
Operator |
Description |
Assignment using Right operand |
= |
Assigns a value to a variable. |
<- |
Assigns a value to a variable. |
<<- |
Assigns a value to a variable from the parent environment. |
Assignment using Right operand |
-> |
Assigns a value to a variable. |
->> |
Assigns a value to a variable from the parent environment. |
Example: #1 - Assignment operator
Print the value of a, b, c andd with various types of Assignment operation.
R Programming Input Screen
# Assignment using Right operand
i = 1
j <- 2
k <<- 3
sprintf("The value of i is %d", i);
sprintf("The value of j is %d", j);
sprintf("The value of k is %d", k);
# Assignment using Right operand
4 -> x
5 ->> y
sprintf("The value of x is %d", x);
sprintf("The value of y is %d", y);
R Programming Output Screen
The value of a is 1
The value of b is 2
The value of c is 3
The value of d is 4
The value of e is 5
R Programming Arithmetic Operator
R Programming provides the Arithmetic operator and it requires two operands to perform the operation. It performs the operations like addition, subtraction, multiplication, division, modulo and floor modulo.
Name |
Operator |
Description |
Addition |
+ |
It adds two numbers i.e. a + b |
subtraction |
- |
It subtracts two numbers i.e. a - b |
Multiplication |
* |
It multiples two numbers i.e. a * b |
Division |
/ |
It divides two numbers i.e. a / b |
Modulo (or) Modulus |
%% |
It returns the remainder of the division operation between two numbers i.e. a / b |
Floor Modulo |
%/% |
It returns the floor remainder of the division operation between two numbers i.e. a / b |
Exponents |
^ |
It returns the exponent i.e. x to the power of y |
Example: #2 - Arithmetic operator
Print the value of a and b with various Arithmetic operation.
R Programming Input Screen
x = 5
y = 3
sprintf("The value of (x + y) is %d", (x + y))
sprintf("The value of (x - y) is %d", (x - y))
sprintf("The value of (x * y) is %d", (x * y))
sprintf("The value of (x / y) is %f", (x / y))
sprintf("The value of (x %% y) is %d", (x %% y))
sprintf("The value of (x %%/%% y) is %d", (x %/% y))
sprintf("The value of (x ^ y) is %d", (x ^ y))
R Programming Output Screen
The value of (x + y) is 8
The value of (x - y) is 2
The value of (x * y) is 15
The value of (x / y) is 1.666667
The value of (x %% y) is 2
The value of (x %/% y) is 1
The value of (x ^ y) is 125
Thousands Lived without Love, but not without water. So SAVE WATER.
R Programming Relational Operator
R Programming provides the Relational operator to compare two operand values of the same data type. It performs the operations like
Logical AND &&,
Logical OR ||
and Logical NOT !.
Name |
Operator |
Description |
Equal to |
== |
Compares two values and returns true only if both are equal. |
Not Equal to |
!= |
Compares two values and returns true only if both are not equal. |
Less than |
< |
Compares two values and returns true only if the left hand side operand is less than to the right hand side operand. |
Greater than |
> |
Compares two values and returns true only if the left hand side operand is greater than to the right hand side operand. |
Less than or equal to |
<= |
Compares two values and returns true only if the left hand side operand is less than or equal to the right hand side operand. |
Greater than or equal to |
>= |
Compares two values and returns true only if the left hand side operand is greater than or equal to the right hand side operand. |
Example: #3 - Relational Operator
Example of Relational like Equal to ==, Not Equal to !=,
Less than <, Greater than >, Less than or equal to <=
and Greater than or equal to >=
R Programming Input Screen
name = "John"
age = 24
# String Comparision
result = name == "John" # true
sprintf("Check the name is John : %s", result)
# Numeric Comparision
result = age == 24 # true
sprintf("Check the person age is 24 : %s", result)
result = age != 20 # true
sprintf("Check the person age is not equal to 20 : %s", result)
result = age < 18 # false
sprintf("Check the person is minor (i.e. less than 18) : %s", result)
result = age > 58 # false
sprintf("Check the person is senior citizen (i.e. greater than 58) : %s", result)
result = age <= 18 # false
sprintf("Check the person age is less than or equal to 18 : %s", result)
result = age >= 24 # true
sprintf("Check the person age is greater than or equal to 24 : %s", result)
R Programming Output Screen
Check the name is John : TRUE
Check the person age is 24 : TRUE
Check the person age is not equal to 20 : TRUE
Check the person is minor (i.e. less than 18) : FALSE
Check the person is senior citizen (i.e. greater than 58) : FALSE
Check the person age is less than or equal to 18 : FALSE
Check the person age is greater than or equal to 24 : TRUE
Thousands Lived without Love, but not without water. So SAVE WATER.
R Programming Logical Operator
R Programming provides the Logical operator and it requires two operands to perform the operation. It performs the operations like Logical AND &&, Logical OR || and Logical NOT !.
Name |
Operator |
Description |
Logical AND |
&& |
Compares two expressions and returns true only if both evaluate to true. |
Logical OR |
|| |
Compares two expressions and returns true either one evaluate to true. |
Logical NOT |
! |
Inverts the boolean value of an expression |
Example: #4 - Logical Operator
Example of Logical AND &&, Logical OR || and Logical NOT !
R Programming Input Screen
john = 32
peter = 19
# Logical AND operation
result1 = (john > 18) && (peter > 18);
result2 = (john < 18) && (peter < 18);
sprintf("Both are above 18 years (Logical AND) : %s", result1);
sprintf("Both are below 18 years (Logical AND) : %s", result2);
# Logical OR operation
result3 = (john > 18) || (peter > 18);
result4 = (john > 30) || (peter > 30);
result5 = (john > 50) || (peter > 50);
sprintf("Anyone is above 18 years (Logical OR) : %s", result3);
sprintf("Anyone is above 30 years (Logical OR) : %s", result4);
sprintf("Anyone is above 50 years (Logical OR) : %s", result5);
# Logical NOT operation
result6 = !(john > 18);
sprintf("Check the john is below 18 years : %s", result6);
R Programming Output Screen
Both are above 18 years (Logical AND) : TRUE
Both are below 18 years (Logical AND) : FALSE
Anyone is above 18 years (Logical OR) : TRUE
Anyone is above 30 years (Logical OR) : TRUE
Anyone is above 50 years (Logical OR) : FALSE
Check the john's age is below 18 years : FALSE
Thousands Lived without Love, but not without water. So SAVE WATER.
R Programming Special Operator
R Programming provides the Special operators and it requires two operands to perform the operation.
It performs the operations like generating sequence of numeric vectors : (i.e. colon operator)
and search operator %in% to identify an element in a vector.
Name |
Operator |
Description |
Sequence Generator |
: |
Generates a Sequence of numeric vector between two operands i.e. 1:5 => 1,2,3,4,5 |
Logical OR |
%in% |
Checks an element is eixst in a vector |
Example: #5 - Special Operator
Generate a sequence between 1 to 5 using : operator.
R Programming Input Screen
# Sequence Generator
x = 1:5
print(x)
R Programming Output Screen
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #6 - Special Operator
Search an element 3 and 6 are exist in a sequence between 1 to 5 using %in% operator.
R Programming Input Screen
# Search
x = 1:5
sprintf("Is 3 is exist in the sequence: %s", 3 %in% x)
sprintf("Is 6 is exist in the sequence: %s", 6 %in% x)
R Programming Output Screen
Is 3 is exist in the sequence: TRUE
Is 6 is exist in the sequence: FALSE
R Programming Unary Operator
R Programming doesn't provides the Unary operators Prefix and Postfix operations but the Increment / Decrement can be achieved in a classical approach.
Name |
Operator |
Description |
Incremental |
|
In R Programming there is no Incremental operator ++, but we can achieve this in an alternate classical approach. |
Decremental |
|
In R Programming there is no Decremental operator --, but we can achieve this in an alternate classical approach. |
Example: #7 - Incremental operator
Print the value of i before increment operation and after increment operation.
R Programming Input Screen
# INCREMENTAL OPERATION
print("Increment Opeation")
i = 6;
sprintf("The value of i before increment is %d", i);
i = i + 1; # increment the value by 1 and assign
sprintf("The value of i before increment is %d", i);
R Programming Output Screen
Increment Opeation
The value of i before increment is 6
The value of i after increment is 7
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #8 - Decremental operator
Print the value of i before decrement operation and after decrement operation.
R Programming Input Screen
# INCREMENTAL OPERATION
print("Increment Opeation")
i = 6;
sprintf("The value of i before decrement is %d", i);
i = i - 1; # decrement the value by 1 and assign
sprintf("The value of i before decrement is %d", i);
R Programming Output Screen
Increment Opeation
The value of i before decrement is 6
The value of i after decrement is 5
Thousands Lived without Love, but not without water. So SAVE WATER.
R Programming Bitwise Operator
R Programming doesn't provide the Bitwise operator, but provides an inbuilt method to perform all these actions. It requires two operands to perform the operation. It performs the operations like Signed Left Shift <<, Unsigned Right Shift >>>, etc.
Name |
Operator |
Description |
Left Shift |
|
Converts the numerical representation to the equivalent binary representation and shifts a 0 into the rightmost n position. In R Programming there is no unsigned left shift operator, but an inbuilt method provides the solution i.e. bitwShiftL() which is equivalent to << |
Unsigned Right Shift |
|
Converts the numerical representation to the equivalent binary representation and shifts a 0 into the leftmost n position. In R Programming there is no unsigned right shift operator, but an inbuilt method provides the solution i.e. bitwShiftR() which is equivalent to >>> |
Bitwise AND |
|
Converts the numerical representation to the equivalent binary representation and perform the logical AND && for each bit against two operands. In R Programming there is no Bitwise Complement operator, but an inbuilt method provides the solution i.e. bitwAnd() which is equivalent to & |
Bitwise OR |
|
Converts the numerical representation to the equivalent binary representation and perform the logical OR || for each bit against two operands. In R Programming there is no Bitwise Complement operator, but an inbuilt method provides the solution i.e. bitwOr() which is equivalent to | |
Bitwise XOR |
|
Converts the numerical representation to the equivalent binary representation and perform the logical XOR for each bit against two operands |
Bitwise Complement |
|
Converts the numerical representation to the equivalent binary representation and perform the logical NOT ! for each bit. In R Programming there is no Bitwise Complement operator, but an inbuilt method provides the solution i.e. bitwNot() which is equivalent to ~ |
Example: #7 - Bitwise Shift Operator
Example of Bitwise operators - Left Shift << and Unsigned Right Shift >>>
R Programming Input Screen
x = -6
y = 6
result = 0
# Left Shift
# Remove the leftmost two binary digits
# and place the binary b'0' to the rightmost 2 position
# to make it 32 bit in length
# x => 11111111 11111111 11111111 11111010 => -6
# x << 2 => 11111111 11111111 11111111 11101000 => -24
# y => 00000000 00000000 00000000 00000110 => 6
# y << 2 => 00000000 00000000 00000000 00011000 => 24
result = bitwShiftL(x, 2)
sprintf("Left Shift (x << 2) is : %d", result)
result = bitwShiftL(y, 2)
sprintf("Left Shift (y << 2) is : %d", result)
# Unsigned Right Shift
# Remove the rightmost two binary digits
# and place the binary b'0' to the leftmost 2 position
# to make it 32 bit in length
# x => 11111111 11111111 11111111 11111010 => -6
# x >>> 2 => 00111111 11111111 11111111 11111110 => 1073741822
# y => 00000000 00000000 00000000 00000110 => 6
# y >>> 2 => 00000000 00000000 00000000 00000001 => 1
result = bitwShiftR(x, 2)
sprintf("Unsigned Right Shift (x >>> 2) is : %d", result)
result = bitwShiftR(y, 2)
sprintf("SUnsigned Right Shift (y >>> 2) is : %d", result)
R Programming Output Screen
Left Shift (x << 2) is : -24
Left Shift (y << 2) is : 24
Unsigned Right Shift (x >>> 2) is : 1073741822
Unsigned Right Shift (y >>> 2) is : 1
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #8 - Bitwise Operator
Example of Bitwise operators - Bitwise AND &, Bitwise OR |, Bitwise XOR ^ and Bitwise Complement ~.
R Programming Input Screen
x = 6
y = 3
result = 0
# Bitwise AND
# x => 00000000 00000000 00000000 00000110 => 6
# y => 00000000 00000000 00000000 00000011 => 3
# x & y => 00000000 00000000 00000000 00000010 => 2
result = bitwAnd(x, y)
sprintf("Bitwise AND (x & y) is : %d", result)
# Bitwise OR
# x => 00000000 00000000 00000000 00000110 => 6
# y => 00000000 00000000 00000000 00000011 => 3
# x | y => 00000000 00000000 00000000 00000111 => 7
result = bitwOr(x, y)
sprintf("Bitwise OR (x | y) is : %d", result)
# Bitwise XOR
# x => 00000000 00000000 00000000 00000110 => 6
# y => 00000000 00000000 00000000 00000011 => 3
# x ^ y => 00000000 00000000 00000000 00000101 => 5
result = bitwXor(x, y)
sprintf("Bitwise XOR (x, y) is : %d", result)
# Bitwise Complement
# x => 00000000 00000000 00000000 00000110 => 6
# ~x => 11111111 11111111 11111111 11111001 => -7
result = bitwNot(x);
sprintf("Bitwise Complement ~x is : %d", result)
R Programming Output Screen
Bitwise AND (x & y) is : 2
Bitwise OR (x | y) is : 7
Bitwise XOR (x ^ y) is : 5
Bitwise Complement ~x is : -7
BBMINFO