R operators perform arithmetic, compare values, assign objects to names, and test logical conditions.
A simple example
Trace two arithmetic operators
Start with7 and 3
Do thisDivide into groups of 3
Result2 groups, 1 left
Integer division counts full groups. The remainder counts the items left over.Lesson.R
cat(7 %/% 3, 7 %% 3, sep = "\n")
Output
2
1
Check a stationery order
A student buys three notebooks at 125 cents each. Add the 50-cent delivery charge once for the whole order. Keeping the arithmetic in cents makes the total easy to check against the receipt.
Types of Operators
Assignment operator
Arithmetic operator
Relational operator
Logical operator
Special operator
Unary operations, bitwise functions, and conditional values
Unary minus changes a sign, and ! negates a logical value. For integer bits, use bitwAnd, bitwOr, or another bitwise function. Use if and else for a scalar choice or ifelse for element-wise selection.
R Programming Assignment Operator
R provides leftward and rightward assignment. The usual form is name <- value.
Name
Operator
Description
Assignment
=
Assigns a value to a variable.
<-
Assigns a value to a variable.
<<-
Searches enclosing environments for an existing binding and assigns there. If no binding is found, assigns in the global environment.
Assignment
->
Assigns a value to a variable.
->>
Performs the same enclosing-environment search as leftward superassignment, with the name on the right.
Example: #1 - Assignment operator
Assign values using different assignment operators and print the results.
R Programming source code
# 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);
Output
[1] "The value of i is 1"
[1] "The value of j is 2"
[1] "The value of k is 3"
[1] "The value of x is 4"
[1] "The value of y is 5"
Advertisements
R Programming Arithmetic Operator
R arithmetic operators generally act element by element on vectors.
Name
Operator
Description
Addition
+
Adds the operands.
subtraction
-
Subtracts the right operand from the left.
Multiplication
*
Multiplies the operands.
Division
/
Divides the left operand by the right.
Modulo (or) Modulus
%%
Returns the remainder after floor division.
Integer division
%/%
Divides and rounds the quotient toward negative infinity.
Exponents
^
Raises the left operand to the power of the right.
Example: #2 - Arithmetic operator
Apply each arithmetic operator to a and b.
R Programming source code
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))
Output
[1] "The value of (x + y) is 8"
[1] "The value of (x - y) is 2"
[1] "The value of (x * y) is 15"
[1] "The value of (x / y) is 1.666667"
[1] "The value of (x %% y) is 2"
[1] "The value of (x %/% y) is 1"
[1] "The value of (x ^ y) is 125"
R Programming Relational Operator
Relational operators compare corresponding elements and produce logical values. Missing values can produce NA.
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 operand is less than the right operand.
Greater than
>
Compares two values and returns true only if the left operand is greater than the right operand.
Less than or equal to
<=
Compares two values and returns true only if the left operand is less than or equal to the right operand.
Greater than or equal to
>=
Compares two values and returns true only if the left operand is greater than or equal to the right operand.
Example: #3 - Relational Operator
Compare the operands for equality and order.
R Programming source code
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)
Output
[1] "Check the name is John : TRUE"
[1] "Check the person age is 24 : TRUE"
[1] "Check the person age is not equal to 20 : TRUE"
[1] "Check the person is minor (i.e. less than 18) : FALSE"
[1] "Check the person is senior citizen (i.e. greater than 58) : FALSE"
[1] "Check the person age is less than or equal to 18 : FALSE"
[1] "Check the person age is greater than or equal to 24 : TRUE"
R Programming Logical Operator
Use & and | for element-wise logical operations. Use && and || for short-circuit scalar conditions. The ! operator negates a logical value.
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
!
Negates the logical value.
Example: #4 - Logical Operator
Compare the results of AND, OR, and NOT.
R Programming source code
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("At least one person is above 18 years (Logical OR) : %s", result3);
sprintf("At least one person is above 30 years (Logical OR) : %s", result4);
sprintf("At least one person is above 50 years (Logical OR) : %s", result5);
# Logical NOT operation
result6 = !(john > 18);
sprintf("John is at most 18 : %s", result6);
Output
[1] "Both are above 18 years (Logical AND) : TRUE"
[1] "Both are below 18 years (Logical AND) : FALSE"
[1] "At least one person is above 18 years (Logical OR) : TRUE"
[1] "At least one person is above 30 years (Logical OR) : TRUE"
[1] "At least one person is above 50 years (Logical OR) : FALSE"
[1] "John is at most 18 : FALSE"
R Programming Special Operator
The colon operator builds a sequence. The %in% operator tests membership.
Name
Operator
Description
Sequence Generator
:
Creates an ascending or descending sequence in steps of one.
Membership
%in%
Tests whether each value on the left occurs among the values on the right.
Example: #5 - Special Operator
Generate the sequence from 1 to 5 with the colon operator.
R Programming source code
# Sequence Generator
x = 1:5
print(x)
Output
[1] 1 2 3 4 5
Example: #6 - Special Operator
Check whether 3 and 6 occur in the sequence from 1 to 5.
R Programming source code
# Search
x = 1:5
sprintf("Is 3 in the sequence?: %s", 3 %in% x)
sprintf("Is 6 in the sequence?: %s", 6 %in% x)
Output
[1] "Is 3 in the sequence?: TRUE"
[1] "Is 6 in the sequence?: FALSE"
Advertisements
Incrementing and decrementing a variable
Increment or decrement a number by assigning the result of addition or subtraction back to the variable.
Name
Operator
Description
Increment
x <- x + 1L
Adds one and stores the result in x.
Decrement
x <- x - 1L
Subtracts one and stores the result in x.
Example: Increment a counter
Print i before and after adding one.
R Programming source code
# INCREMENTAL OPERATION
print("Increment operation")
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 after increment is %d", i);
Output
[1] "Increment operation"
[1] "The value of i before increment is 6"
[1] "The value of i after increment is 7"
Example: Decrement a counter
Print i before and after subtracting one.
R Programming source code
# Decrement
print("Decrement operation")
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 after decrement is %d", i);
Output
[1] "Decrement operation"
[1] "The value of i before decrement is 6"
[1] "The value of i after decrement is 5"
R Programming Bitwise Operator
R provides integer bit operations through functions such as bitwAnd, bitwOr, bitwXor, and bitwNot.
Name
Operator
Description
Left Shift
bitwShiftL
Shifts the integer bit pattern left.
Unsigned Right Shift
bitwShiftR
Shifts the integer bit pattern right, filling with zero bits.
Bitwise AND
bitwAnd
Keeps bits set in both operands.
Bitwise OR
bitwOr
Keeps bits set in either operand.
Bitwise XOR
bitwXor
Keeps bits that differ between the operands.
Bitwise Complement
bitwNot
Flips each bit.
Example: #7 - Bitwise Shift Operator
Compare bitwShiftL and bitwShiftR.
R Programming source code
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("Unsigned Right Shift (y >>> 2) is : %d", result)
Output
[1] "Left Shift (x << 2) is : -24"
[1] "Left Shift (y << 2) is : 24"
[1] "Unsigned Right Shift (x >>> 2) is : 1073741822"
[1] "Unsigned Right Shift (y >>> 2) is : 1"
Example: #8 - Bitwise Operator
Compare bitwise AND, OR, XOR, and complement.
R Programming source code
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)
Output
[1] "Bitwise AND (x & y) is : 2"
[1] "Bitwise OR (x | y) is : 7"
[1] "Bitwise XOR (x, y) is : 5"
[1] "Bitwise Complement ~x is : -7"