R Operators

In this chapter you will learn about the various types of operators that R language allows programmers to use. They play an important role in calculating values of constants and variables.

What are Operators in R?

The operators are those symbols which tell the compiler for performing precise mathematical or logical manipulations. R programming is loaded with built in operators and supplies below mentioned types of operators.

Types of Operators
  • The Arithmetic Operators
  • The Relational Operators
  • The Logical Operators
  • The Assignment Operators

The below mentioned table gives the arithmetic operators hold up by R language. The operators act on each element of the vector.

Arithmetic Operators

g <- c (4, 6.5, 6)

s <- c (8, 3, 5)

print (g + s)

Subtract

g <- c ( 2, 5.5, 6)

s <- c (8, 3, 4)

print (g - s)

Multiply

g <- c ( 26.5,8)

s <- c(6, 4, 3)

print (g * s)

Divide

g <- c( 2,4.6,8)

s <- c(8, 4, 3)

print (g / s)

Modulus

g <- c ( 2, 5.5, 8)

s <- c (8, 4, 5)

print (g% / %s)

Relational Operators

Greater than

g <- c (2,5.5,6,9)

s <- c (8,2.5,14,9)

print (g > s)

Less than

g <- c (2, 5.6, 6,9)

s <- c(8,2.5,14,9)

print (g < s)

Equals Operator

g <- c (2,5.5,6,9)

s <- c (8,2.5,14,9)

print (g == s)

Less than or equal

g <- c (2, 5.5, 6, 9)

s <- c (8, 2.5, 14, 9)

print (g <= s)

Greater than or equal

g <- c(2,5.5,6,9)

s <- c(8, 2.5, 14, 9)

print(g>=s)

Not equal

g <- c(2, 5.4, 8, 9)

s <- c(8, 2.5, 14, 8)

print(v!=t)

Logical Operators

Here are the set of logical operators that R language allows to use. This operator is valid only to vectors of type logical, number or complex numbers. All figures greater than one is considered to be logical value i.e. TRUE.

Element-wise Logical AND Operator

g <- c(3, 1, TRUE, 2+3i)

s <- c(4,1,FALSE, 2+3i)

print (g & s)

It unites each element of the 1st vector with the equivalent element of the 2nd vector and returns TRUE or FALSE.

Element-wise Logical OR Operator

g <- c(3,0, TRUE, 2+2i)

s <- c(4,0, FALSE, 2+3i)

print (g | s)

It unites each element of the 1st vector with the equivalent element of the 2nd vector.

Logical NOT Operator

k <- c (3,0, TRUE, 2+2i)

print (!k)

Logical AND Operator

g <- c(3,0,TRUE,2+2i)

s <- c(1,3,TRUE,2+3i)

print (g && s)

Logical OR Operator

g <- c (0,0,TRUE,2+2i)

s <- c (0,3,TRUE,2+3i)

print (g||s)

Assignment Operator

There are three types of operators used for assigning values to vectors.

g1 <- c (2,1,TRUE, 2+3i)

g2 <<- c (2,1,TRUE, 2+3i)

g3 = c (2,1, TRUE, 2+3i)

print (g1)

print (g2)

print (g3)