R Operators

R Operators

An Operator is a symbol that tells to perform different operations between operands. R programming is very rich in built-in operators.

R has the following data operators:

  • Arithmetic Operators
  • Assignment Operators
  • Logical Operators
  • Relational Operators
  • Miscellaneous Operators

Let us discuss the types of operators in detail.

Arithmetic Operators

These operators perform basic arithmetic operations like addition, subtraction, multiplication, division, exponent, modulus, etc.

Let us see what these operators do.

For example, take two variables:

For example:-

x <- 10
y <- 5
Operator Operation Output
x+y Addition 15
x – y Subtraction 5
x * y Multiplication 50
x / y Division 2
x ^ y Exponent 10^5
x %% y Modulus 0

These operators can also be used to carry out mathematical operations on vectors.

Lead the Data Science Industry with Confidence
Master Data Science with Us
quiz-icon

For creating vectors, we use the c() function.

In the case of vectors, all these operations are done in an element-by-element fashion.

For example:

x  <-  c(9,9,9)
y  <-  c(1,1,1)
print(x+y)
Output: [1]  10 10 10

Let us see how you can use these arithmetic operators in R programming.

  • Addition

a <- c(8,4.2,7)

b <- c(2, 4, 3)

print(a+b)

Output:

[1] 10.0  8.2 10.0

  • Subtraction

a <- c(4,7.8,7)

b <- c(6, 4, 2)

print(a-b)

Output:

[1] -2.0  3.8  5.0

  • Multiplication

a <- c(4,6.2,7)

b <- c(8, 2, 6)

print(a*b)

Output:

[1] 32.0 12.4 42.0

  • Division

a <- c(6,5.5,8)

b <- c(12, 2, 4)

print(a/b)

Output:

[1] 0.50 2.75 2.00

  • Exponent

a <- c(3,5.5,4)

b <- c(2, 4, 3)

print(a^b)

Output:

[1]   9.0000 915.0625  64.0000

  • Modulus

a <- c(3,7.5,8)

b <- c(6, 4, 4)

print(a%%b)

Output:

[1] 3.0 3.5 0.0

Lead Data Science with our free course.
Enroll in Our Free Data Science Program
quiz-icon

Assignment Operators

The use of these operators is to assign values to the variables. There are two kinds of assignments, leftwards assignment, and rightwards assignment.
Operators ‘<-‘ and  ‘=’ are used to assign values to any variable.
x<- 3 or x = 3 (Leftwards Assignment)
3 -> x or x = 3 (Rightwards Assignment)

Logical Operators

These operators are used to perform Boolean operations like AND, OR, NOT, etc. on variables.
Different logical operators are as follows:

!

NOT
&

AND (Element wise)
&&

AND
|

OR (Element wise)
||

OR

!

NOT

ZEROS are taken as FALSE and NON-ZERO numbers are taken as TRUE.
For example:

x &amp;lt;- c (FALSE, TRUE,3,0)
y &amp;lt;- c (FALSE, TRUE, FALSE, TRUE)
!x&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (NOT operation)
x&amp;amp;y&amp;nbsp;&amp;nbsp;&amp;nbsp; (AND operation)
Output:
[1] TRUE FALSE FALSE TRUE
[1] FALSE TRUE FALSE FALSE
and,x&amp;amp;&amp;amp;y
x||y
Output:
[1] FALSE
[1] FALSE

Relational Operators

These operators are used to compare two values or variables. To find if one is smaller, greater, equal, not equal, and other similar operations these operators are used. The output of a
A relational operator is always a Logical value, that is either TRUE or FALSE.
For example:-
x <- 10
y <- 5

Greater than x>y Output: TRUE
Less than x<y Output: FALSE
Greater than and equal to x>=y Output: TRUE
Less than and equal to x<=y Output: FALSE
Equal to x==y Output: FALSE
Not equal to x!=y Output: TRUE

Miscellaneous Operators

These R programming operators are used for special cases and are not for general mathematical or logical computation.
colon operator – It is used to generate a series of numbers in sequence for a vector.
For example:

x &amp;lt;- 0:9
print(x)
Output:[1] 0 1 2 3 4 5 6 7 8 9
%in% :-&amp;nbsp; This operator is used to check if an element belongs to a vector or not.

Get 100% Hike!

Master Most in Demand Skills Now!

For example:

v1 &amp;lt;- 5
t &amp;lt;- 0:9
print(v1 %in% t)
Output:[1] TRUE

%* %: This operator multiplies a matrix with its transpose.
For example:

M = matrix( c(1,2,3,4), nrow = 2,ncol = 2,byrow = TRUE)
T= M %*% t(M)
print(T)
Output: [,1] [,2]
[1,]&amp;nbsp;&amp;nbsp;&amp;nbsp; 5&amp;nbsp;&amp;nbsp; 11
[2,]&amp;nbsp;&amp;nbsp; 11&amp;nbsp;&amp;nbsp; 25

In this tutorial, we learned about the different R programming operators and how to use these operators to perform various arithmetic and logical manipulations in R. This knowledge is fundamental for anyone pursuing Data Science online training, as it enables you to efficiently analyze and manipulate data using R. Understanding operators like arithmetic, comparison, and logical operators will help you perform key tasks such as data transformation, statistical analysis, and model evaluation.

Our Data Science Courses Duration and Fees

Program Name
Start Date
Fees
Cohort starts on 1st Feb 2025
₹65,037
Cohort starts on 25th Jan 2025
₹65,037
Cohort starts on 11th Jan 2025
₹65,037

About the Author

Principal Data Scientist

Meet Akash, a Principal Data Scientist with expertise in advanced analytics, machine learning, and AI-driven solutions. With a master’s degree from IIT Kanpur, Aakash combines technical knowledge with industry insights to deliver impactful, scalable models for complex business challenges.