R Operators

Operators are used to perform operations on values and variables.

The R operators are classified into six different categories:

  • Arithmetic operators
  • Comparison operators
  • Logical operators
  • Element-wise Logical operators
  • Membership operators
  • Assignment operators

Arithmetic Operators

Arithmetic operators are used to perform simple mathematical operations on numeric values and vectors.

Arithmetic operators in R
OperatorMeaningExample
+Additionx + y
Subtractionx – y
*Multiplicationx * y
/Divisionx / y
%%Modulusx %% y
^Exponentsx ^ y
%/%Integer divisionx %/% y

Here are some examples:

Example: Operations on numeric values

> x <- 6
> y <- 2

> # addition
> x + y
[1] 8

> # subtraction
> x - y
[1] 4

> # multiplication
> x * y
[1] 12

> # division
> x / y
[1] 3

> # modulus
> x %% y
[1] 0

> # exponents
> x ^ y
[1] 36

> # integer division
> x %/% y
[1] 3

All the basic arithmetic operators can be performed on pairs of vectors.

Each operation is performed in an element-by-element manner.

Example: Operations on vectors

> v1 <- c(11,12,13,14,15)
> v2 <- c(1,2,3,4,5)

> # addition
> v1 + v2
[1] 12 14 16 18 20

> # subtraction
> v1 - v2
[1] 10 10 10 10 10

> # multiplication
> v1 * v2
[1] 11 24 39 56 75

> # division
> v1 / v2
[1] 11.000000  6.000000  4.333333  3.500000  3.000000

> # exponents
> v1 ^ v2
[1]     11    144   2197  38416 759375

Comparison Operators

Comparison operators are used to compare two values or vectors.

Comparison operators in R
OperatorMeaningExample
==Equal tox == y
!=Not equal tox != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Here are some examples:

Example: Operations on numeric values

> x <- 6
> y <- 2

> # equal to
> x == y
[1] FALSE

> # not equal to
> x != y
[1] TRUE

> # greater than
> x > y
[1] TRUE

> # less than
> x < y
[1] FALSE

> # greater than or equal to
> x >= y
[1] TRUE

> # less than or equal to
> x <= y
[1] FALSE

Example: Operations on vectors

> v1 <- c(11,2,13,4,15)
> v2 <- c(1,12,3,14,5)

> # equal to
> v1 == v2
[1] FALSE FALSE FALSE FALSE FALSE

> # not equal to
> v1 != v2
[1] TRUE TRUE TRUE TRUE TRUE

> # greater than
> v1 > v2
[1]  TRUE FALSE  TRUE FALSE  TRUE

> # less than
> v1 < v2
[1] FALSE  TRUE FALSE  TRUE FALSE

> # greater than or equal to
> v1 >= v2
[1]  TRUE FALSE  TRUE FALSE  TRUE

> # less than or equal to
> v1 <= v2
[1] FALSE  TRUE FALSE  TRUE FALSE

Logical Operators

Logical operators are used to join two or more conditions.

R Logical operators
OperatorDescriptionExample
&&Returns True if both statements are truex > 0 && y < 0
||Returns True if one of the statements is truex > 0 || y < 0
!Reverses the result, returns False if the result is true!(x > 0 && y < 0)

Example:

> x <- 2
> y <- -2

> # and
> x > 0 && y < 0
[1] TRUE

> # or
> x > 0 || y < 0
[1] TRUE

> # not
> !(x > 0 && y < 0)
[1] FALSE

Element-wise Logical Operators

These operators are used to perform logical operations on vectors in an element-by-element manner.

R element-wise logical operators
OperatorDescriptionExample
&Returns True if respective elements of both vectors are truev1 && v2
|Returns True if one of the respective elements of both vectors is truev1 || v2

Here are some examples:

Example:

> v1 <- c(0,1,2,3,4)
> v2 <- c(0,1,0,1,0)

> # and
> v1 & v2
[1] FALSE  TRUE FALSE  TRUE FALSE

> # or
> v1 | v2
[1] FALSE  TRUE  TRUE  TRUE  TRUE

Membership Operator

Membership operator is used to check if a specific item is present in the vector or the list.

R Membership operators
OperatorDescriptionExample
%in%Returns True if a value is present in the vector or the listx %in% y

Here are some examples:

Example:

> v <- list("red", "green", "blue")

> "red" %in% v
[1] TRUE

> "blue" %in% v
[1] TRUE

Assignment Operators

Assignment operators are used to assign new values to variables.

Assignment operators in R
OperatorMeaningExample
=Assignmentx = 3
<-, <<-, =Leftwards assignmentx <- 3, x <<- 3, x = 3
->, ->>Rightwards assignment3 -> x, 3 ->> x

Here are some examples:

Example:

> x <- 3
> x
[1] 3

> x = 5
> x
[1] 5

> 9 -> x
> x
[1] 9

Miscellaneous Operators

These operators are used to for special purposes.

OperatorDescriptionExample
:Generates a number sequence from a to b1:10
%*%Multiplies two matricesm1 %*% m2

Here are some examples:

Example:

> # create a sequence
> 1:10
[1]  1  2  3  4  5  6  7  8  9 10

> # multiplies two matrices
> m1 <- matrix( c(1,2,3,4), nrow=2, ncol=2, byrow=TRUE)
> m2 <- matrix( c(10,20,30,40), nrow=2, ncol=2, byrow=TRUE)
> m1 %*% m2
     [,1] [,2]
[1,]   70  100
[2,]  150  220

Operator Precedence (Order of Operations)

In R, every operator is assigned precedence.

Operator Precedence determines which operations are performed before which other operations.

Operators of highest precedence are performed first.

Order of precedence of the R operators
OperatorDescription
highest precedence( {Function calls and grouping expressions (respectively)
[ [[Indexing
:: :::Access variables in a namespace
$ @Component / slot extraction
^Exponentiation (right to left)
– +Unary minus and plus
:Sequence operator
%any%Special operators
* /Multiply, divide
+ –(Binary) add, subtract
< > <= >= == !=Ordering and comparison
!Negation
& &&And
| ||Or
~As in formulas
-> ->>Rightward assignment
=Assignment (right to left)
<- <<-Assignment (right to left)
lowest precedence?Help (unary and binary)