Operators in R

Operators are symbols used to operate on given variables. For example, “+” is an operator used to add values. Operators in R can be classified into four types. This guide will discuss each of these operators.

Types of Operators
Four types of operators in R.

Arithmetic Operators

These operators carry out mathematical operations, like addition, subtraction, multiplication, division, and modulus. Arithmetic Operators include:

  • Addition(+): to add
  • Subtraction(-): to subtract
  • Multiplication(*): to multiply
  • Division(/): to divide
  • Modulus(%%): to return the remainder

Example

Suppose you have two variables, x and y.

#creating variables
x<-21
y<-3
x+y #addition operator
x-y #subtraction operator
x*y #multiply operator
x/y #division operator
x%%y #modulus operator

Output

[1] 24
[1] 18
[1] 63
[1] 7
[1] 0

Relational Operators

These operators are used for making comparisons. The output of a relational operator is either TRUE or FALSE. Types of Relational Operators include:

  • Greater than(>): Returns TRUE if the first operand is greater than the second; else returns FALSE.
  • Less than(<): Returns TRUE if the first operand is less than the second; else returns FALSE.
  • Greater than equal to (>=): Returns TRUE if the first operand is greater than or equal to the second; else returns FALSE.
  • Less than equal to (<=): Returns TRUE if the first operand is less than or equal to the second; else returns FALSE.
  • Not equal to (!=): Returns TRUE if the first operand is not equal to the second; else returns FALSE.
  • Equal to (==): Returns TRUE if the first operand is equal to the second; else returns FALSE.

Example

Suppose you have two variables, x and y.

#creating variables
x<-30
y<-6
x>y #greater than
x<y #less than
x>=y #greater than or equal
x<=y #less than or equal
x!=y #not equal to
x==y #equal to

Output

[1] TRUE
[1] FALSE
[1] TRUE
[1] FALSE
[1] TRUE
[1] FALSE

Assignment Operators

These operators are used to assign values to the variables. Types of Assignment Operators

  • Left Assignment(<− or = or <<−)
  • Right Assignment(-> or ->>)

Example

x<- 6 #left assignment
print(x)
6->x #right assignment
print(x)

Output

[1] 6
[1] 6

There is no difference between the two types of assignment operators.

Logical Operators

These operators are used for logical operations like AND, OR, and NOT on the variables. These operators apply only to vectors of data type logical, numeric or complex. Zero is considered FALSE, and a non-zero number is TRUE. There are the main types of Logical Operators:

Element-wise AND Operator (&)

It takes two logical values and returns TRUE only if both the logical values are true themselves. When applied to vectors, it performs an element-wise operation on each pair of elements from both vectors and returns true if both the elements are true.

#creating variable x
x <- 11
y <- 22
#check if x is greater than 7 and less than 14
x>7 & x<14
#check if y is greater than 7 and less than 14
y>10 & y<20
#using & operator in vectors
c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)

Output

[1] TRUE
[1] FALSE
[1] TRUE FALSE FALSE

In the above example, the first condition returns true because both the operands x>7 and x<14 evaluate as true. The second condition returns false as the first operand y>10 is true, but the second operand y<20 is false. While working with vectors, the first elements in both vectors are true, so the first element of the resulting vector is true; similarly, for the second element, where true and false result in false.

Element-wise OR Operator (|)

It takes two logical values and returns TRUE if one of the logical values is true themselves. When applied to vectors, it performs an element-wise operation on each pair of elements from both vectors and returns true if one of the elements is true.

#creating variable x
x <- 10
y <- 20
#check if x is greater than 7 and less than 14
x>12 | x<14
#check if y is greater than 7 and less than 14
y>25 | y<20
#using | operator in vectors
c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)

Output

[1] TRUE
[1] FALSE
[1] TRUE TRUE FALSE

In the above example, the first condition returns true because one of the operands is true. The second condition returns false as both the operands are false. While working with vectors, the first elements in both vectors are true, so the first element of the resulting vector is true; similarly, for the second element, where one is true, and the other is false, results in true.

Logical NOT Operator (!)

This operator negates the value it’s used on.

!TRUE
!FALSE
is.numeric(9)
!is.numeric(9)

Output

[1] FALSE
[1] TRUE
[1] TRUE
[1] FALSE

In the above example, is.numeric(9) returned true as 9 is of numeric data type. To negate this, we used an exclamation mark and returned false.

Logical AND Operator (&&)

This operator operates on the first elements from both vectors and returns true if both the first elements are true.

c(TRUE, TRUE, FALSE) && c(FALSE, TRUE, FALSE)

Output

[1] FALSE

Here, we used the && operator; therefore, it operated only on both vectors’ first element. The first elements in both vectors are different, so the resulting vector is false.

Logical OR Operator (||)

This operator operates on the first elements from both vectors and returns true if one of the first elements is true.

c(TRUE, TRUE, FALSE) || c(FALSE, TRUE, FALSE)

Output

[1] FALSE

Here, we used the || operator; therefore, it operated only on the first element of both vectors. Thus, one of the first elements in both vectors is true, so the resulting vector is true.

Leave a Comment