R Operators [Chapter 1 Episode 4]

In R, an operator is a symbol that instructs the interpreter to perform a certain mathematical or logical operation. R comes with several built-in operators that you can use for such tasks. R operators are classified as follows:

  • Arithmetic Operator

  • Relational Operator

  • Logical Operator

  • Assignment Operator

Arithmetic Operator

These are operators used to perform mathematical operations like addition, subtraction, division, multiplication etc.

Addition

x <- 4

y <- 7

cat("x + y returns: ",x+y,"\n")

output

x + y returns:  11

Subtraction

x <- 14

y <- 8

cat("x - y returns: ",x-y,"\n")

output

x - y returns:  6

Division

x <- 19

y <- 7

cat("x / y returns: ",x / y,"\n")

output

x / y returns:  2.714286

Multiplication

x <- 10

y <- 7

cat("x * y returns: ",x * y,"\n")

output

x * y returns:  70

Modulus

x <- 10

y <- 7

cat("x %% y returns: ",x %% y,"\n")

output

x %% y returns:  3

Floor

x <- 10

y <- 7

cat("x %/% y returns: ",x %/% y,"\n")

output

x %/% y returns:  1

Exponential

x <- 3

y <- 4

cat("x ^ y returns: ",x ^ y,"\n")

output

x ^ y returns:  81

Relational Operators

Relational Operators allows you to make comparison between values. Here is a list of operators supported in R:

  • < : less than

  • > : greater than

  • <= : less than or equal to

  • >= : greater than or equal to

  • == : equal to

- != : not equal to

Logical Operators

We use these operators to perform Boolean operations such as AND, OR etc. Logical operators are only applicable to numeric, logical or complex vectors. Here are the logical operators supported by R:

  • ! : Logical NOT

  • & : Element-wise logical AND

  • && : Logical AND

  • | : Element-wise logical OR

  • || : Logical OR

The & and | operators perform an element-wise operation to give a result having the length of the longer operand. The && and || will examine only the first element of the operands and returns a single length logical vector. Zero is considered to be FALSE while non-zero numbers are treated as TRUE.

x <- 7

y <- 13

cat("x > y OR x < y returns: ",x > y | x < y,"\n")

output

x > y OR x < y returns:  TRUE
x <- 7

y <- 13

cat("x > y AND x < y returns: ",x > y && x < y,"\n")

output

x > y AND x < y returns:  FALSE
x <- c(TRUE, FALSE, 0, 7)
y <- c(FALSE, TRUE, FALSE, TRUE)
cat("!x returns: ",!x,"\n") 
cat("x&y returns: ",x&y,"\n") 
cat("x&&y returns: ",x&&y,"\n") 
cat("x|y returns: ",x|y,"\n") 
cat("x||y returns: ",x||y,"\n")
!x returns:  FALSE TRUE TRUE FALSE  #reverses the operator i.e returns True for False and False for True
x&y returns:  FALSE FALSE FALSE TRUE
x&&y returns:  FALSE 
x|y returns:  TRUE TRUE FALSE TRUE
x||y returns:  TRUE

Assignment Operator

We use assignment operator to assign values to variables in R.

  • <-, <<-, = : leftwards assignment

  • ->, ->> : rightwards assignment

The = and <- operators can be used interchangeably to assign a variable a value in the same environment. The <<- operator should be used to assign values in parent environments similar to global assignments. The rightward assignments are not very commonly used even though they are available. The following example demonstrates how to use these operators:

x <- 15

a <- 21

17 -> y

output

> x <- 15
> x
[1] 15
> a <- 21
> a
[1] 21
> 17 -> y
> y
[1] 17

Order of Operation in R are PEMDAS i.e Parenthesis, Exponential, Multiplication, Division, Addition, Subtraction.