Operators in R Programming

Filed Under: R Programming
Operators In R

Operators in a programming language are certain tokens or symbols that perform specific operations on the arguments supplied. The arguments are known as operands. These operations are usually arithmetic or logical in nature. R supports various operators across different categories.

R Operators Classification

We can classify R operators into the following categories.

  1. Arithmetic
  2. Relational
  3. Logical
  4. Assignment
  5. Special operators

1. Arithmetic Operators in R

These are the operators that perform basic arithmetic operations like addition, subtraction, multiplication, and division. In addition, there are also operators for modulus and exponentiation.

These operators work on both single variables and vectors. The use of these operators is straightforward.

The following code snippet will help you gain an understanding of these operators.

#Mathemetical operations in single numeric values.
> a=2
> b=4
#Addition -- Operator +
> a+b
[1] 6
#Subtraction -- Operator -
> b-a
[1] 2
#Multiplication -- Operator *
> a*b
[1] 8
#Division -- Operator /
> b/a
[1] 2

Similarly, we also have operators for exponentiation ^ and modulo %%. These are illustrated below. The modulo operator gives the remainder of a division operation.

#Modulo Operator - %%
> x=10
> y=4
> z=x%%y
> z
[1] 2
#Exponentiation Operator - ^
> w=x^y
> w
[1] 10000

These operators also work on a vector level. Addition, subtraction and other operations work on an element by element level. This is illustrated in the code below.

#Define 2 vectors
> vec1=c(2,3,5,1,4)
> vec2=c(3,4,5,1,7)
#Addition to two vectors
> vec1+vec2
[1]  5  7 10  2 11
#Subtraction of two vectors
> vec1-vec2
[1] -1 -1  0  0 -3
#Multiplication of two vectors
> vec1*vec2
[1]  6 12 25  1 28
#Division of two vectors
> vec1/vec2
[1] 0.6666667 0.7500000 1.0000000 1.0000000 0.5714286
#Exponentiation of a vector using integer
> vec1^2
[1]  4  9 25  1 16
#Element-wise exponentiation
> vec1^vec2
[1]     8    81  3125     1 16384

2. Relational Operators in R

These are used to compare two numbers or two vectors. The main relational operators are greater than (>) , less than (<) and equal to (==).

There are other compound operators greater than or equal to (>=) and less than or equal to(<=). The output of these operators is a Boolean value, TRUE or FALSE. These are illustrated in the given code snippet.

When applied to vectors, these operators perform an element-wise comparison and give a vector of corresponding Boolean values as a result.

> x
[1] 10
> y
[1] 4
> z
[1] 2
> w
[1] 10000
> x < w
[1] TRUE
> y > z
[1] TRUE
> x == w
[1] FALSE
> x <= x
[1] TRUE
> y >= z
[1] TRUE
> vec1<vec2 #From the previous operator set.
[1]  TRUE  TRUE FALSE FALSE  TRUE

3. R Logical Operators

These take Boolean values as operands and give Boolean value as output. These are AND (&), OR (|) and NOT (!). These can be used as follows:

#Logical AND - Gives TRUE only if both the holding conditions are TRUE.
> 2<4&4>1
[1] TRUE
> 8>8&9>8
[1] FALSE

#Logical OR - Gives TRUE if any of the holding condtions are TRUE.
> (1.3-3.14<0)|(3==0)
[1] TRUE

#Logical NOT - Simply negates the value from FALSE to TRUE or TRUE to FALSE.
> !(30>2)
[1] FALSE

Similar to the logical operators above, we also have element-wise logical operators – AND (&&) and OR (||). These are used upon vectors and give results corresponding to only the first elements of two vectors. This can be shown in the following code.

> vec1
[1] 2 3 5 1 4
> vec3
[1] 1 0 0 0 0

> vec1&vec3
[1]  TRUE FALSE FALSE FALSE FALSE
> vec1&&vec3
[1] TRUE

> vec1|vec3
[1] TRUE TRUE TRUE TRUE TRUE
> vec1||vec3
[1] TRUE
> !vec1
[1] FALSE FALSE FALSE FALSE FALSE

4. R Assignment Operators

The assignment operators are used to assign values to a variable. There are three assignment operators in R, left assignment (<-), right assignment (->) and equal to (=). These have been discussed in detail in the variables tutorial here.

5. Special Operators in R

R has some operators defined for special purposes. Some of these are:

  • Sequence Generator – Specified by a colon symbol (:)
    • Used to define a sequence range.
    • 2:10 – means a sequence from 2 to 10.
  • List Indexing Operator – Specified by a dollar sign $.
    • Facilitates extracting an element of a list by its name.
  • Model Formula Operator – Specified by a tilde sign ~
    • Used to define a re-usable formula along with its dependent variables.

We will delve deeper into the list indexing and model formula operators in our further advanced tutorials. The sequence generator works as follows.

> seq2 <- 2:10
> seq2
[1]  2  3  4  5  6  7  8  9 10

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages