R Operators

➕➖✖️➗ 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:
Arithmetic Operators
Relational (Comparison) Operators
Logical Operators
Assignment Operators
Miscellaneous Operators
3️⃣ Arithmetic Operators ➕➖✖️➗
Used for mathematical calculations.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 2 | 7 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 5 * 2 | 10 |
/ | Division | 5 / 2 | 2.5 |
^ | Power | 5 ^ 2 | 25 |
%% | Modulus | 5 %% 2 | 1 |
%/% | Integer Division | 5 %/% 2 | 2 |
4️⃣ Relational (Comparison) Operators 🔍
Used to compare values.
They return TRUE or FALSE.
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | 5 == 5 |
!= | Not equal | 5 != 3 |
> | Greater than | 5 > 3 |
< | Less than | 5 < 3 |
>= | Greater or equal | 5 >= 5 |
<= | Less or equal | 5 <= 4 |
5️⃣ Logical Operators 🔗
Used to combine conditions.
Element-wise Logical Operators
| Operator | Meaning |
|---|---|
& | AND |
| ` | ` |
! | NOT |
Short-circuit Logical Operators ⭐
| Operator | Meaning |
|---|---|
&& | AND (first element only) |
| ` |
📌 Commonly used in if conditions.
6️⃣ Assignment Operators 📝
Used to assign values to variables.
| Operator | Usage |
|---|---|
<- | 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:
^*,/+,-Relational operators
Logical operators
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 analysisCore topic for R exams, interviews & data science
