R Operators

R Tutorial

➕➖✖️➗ R Operators

In R, operators are symbols that tell R to perform operations on variables and values.
They are fundamental for data analysis, statistics, conditions, and programming logic.


1️⃣ What are Operators in R?

Operators are used to:

  • Perform calculations

  • Compare values

  • Combine conditions

  • Assign values to variables

📌 R has multiple types of operators, each with a specific purpose.


2️⃣ Types of Operators in R ⭐

R operators are broadly classified into:

  1. Arithmetic Operators

  2. Relational (Comparison) Operators

  3. Logical Operators

  4. Assignment Operators

  5. Miscellaneous Operators


3️⃣ Arithmetic Operators ➕➖✖️➗

Used for mathematical calculations.

OperatorMeaningExampleResult
+Addition5 + 27
-Subtraction5 - 23
*Multiplication5 * 210
/Division5 / 22.5
^Power5 ^ 225
%%Modulus5 %% 21
%/%Integer Division5 %/% 22

4️⃣ Relational (Comparison) Operators 🔍

Used to compare values.
They return TRUE or FALSE.

OperatorMeaningExample
==Equal to5 == 5
!=Not equal5 != 3
>Greater than5 > 3
<Less than5 < 3
>=Greater or equal5 >= 5
<=Less or equal5 <= 4


 


5️⃣ Logical Operators 🔗

Used to combine conditions.

Element-wise Logical Operators

OperatorMeaning
&AND
``
!NOT

 


Short-circuit Logical Operators ⭐

OperatorMeaning
&&AND (first element only)
`
TRUE && FALSE # FALSE
TRUE || FALSE # TRUE

📌 Commonly used in if conditions.


6️⃣ Assignment Operators 📝

Used to assign values to variables.

OperatorUsage
<-Assignment (most common)
=Assignment
->Right assignment
<<-Global assignment


 


7️⃣ Miscellaneous Operators ⭐

Sequence Operator :



 


Membership Operator %in%



 


Matrix Multiplication %*%


 


8️⃣ Operator Precedence (Order of Execution) ⭐

High to Low priority:

  1. ^

  2. *, /

  3. +, -

  4. Relational operators

  5. Logical operators

5 + 2 * 3 # 11 (not 21)

9️⃣ Common Mistakes ❌

❌ Using = instead of == for comparison
❌ Confusing & with &&
❌ Forgetting operator precedence
❌ Using %/% instead of / unintentionally


📌 Interview Questions & MCQs (Very Important)

Q1. Which operator is used for assignment in R?

A) =
B) <-
C) ->
D) All of the above

Answer: D


Q2. What does %% operator do?

A) Division
B) Power
C) Modulus
D) Integer division

Answer: C


Q3. Difference between & and &&?

A) No difference
B) && works element-wise
C) & works element-wise
D) Both same

Answer: C


Q4. What does %in% do?

A) Assignment
B) Comparison
C) Membership test
D) Loop

Answer: C


Q5. Output of 5 %/% 2?

A) 2.5
B) 3
C) 2
D) 1

Answer: C


🔥 Real-Life Use Cases

✔ Data filtering
✔ Conditional logic
✔ Statistical calculations
✔ Vector & matrix operations
✔ Data science workflows


✅ Summary

  • Operators perform actions on data

  • R supports arithmetic, logical, relational, assignment & special operators

  • & vs && is very important

  • %in% is widely used in data analysis

  • Core topic for R exams, interviews & data science

You may also like...