⚙️ R Operators – Arithmetic, Relational, Logical, and Assignment Operators
🧲 Introduction – What Are Operators in R?
Operators in R are special symbols or keywords that perform calculations, comparisons, logical decisions, or assign values. They form the backbone of expressions and conditional logic in any R program.
Whether you’re analyzing numbers, checking conditions, or joining text, understanding how R operators work is essential for effective coding and data manipulation.
🎯 In this guide, you’ll learn:
- Categories of operators in R
- Arithmetic, relational, logical, and assignment operators
- Use of special operators like
%in%and:(sequence) - Practical examples with expected output
➕ 1. Arithmetic Operators
Used for basic mathematical calculations:
| Operator | Description | Example | Result |
|---|---|---|---|
+ | Addition | 3 + 2 | 5 |
- | Subtraction | 7 - 3 | 4 |
* | Multiplication | 4 * 2 | 8 |
/ | Division | 10 / 2 | 5 |
^ or ** | Exponentiation | 2 ^ 3 | 8 |
%% | Modulus (remainder) | 7 %% 3 | 1 |
%/% | Integer Division | 7 %/% 3 | 2 |
🔍 2. Relational (Comparison) Operators
Return TRUE or FALSE by comparing values:
| Operator | Description | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | TRUE |
!= | Not equal to | 5 != 3 | TRUE |
> | Greater than | 5 > 2 | TRUE |
< | Less than | 3 < 5 | TRUE |
>= | Greater than or equal | 4 >= 4 | TRUE |
<= | Less than or equal | 3 <= 2 | FALSE |
🔗 3. Logical Operators
Used for combining Boolean values or logical conditions:
| Operator | Description | Example | Result |
|---|---|---|---|
& | AND (element-wise) | TRUE & FALSE | FALSE |
| ` | ` | OR (element-wise) | `TRUE |
! | NOT | !TRUE | FALSE |
&& | AND (first element only) | TRUE && FALSE | FALSE |
| ` | ` | OR (first element only) |
🎯 4. Assignment Operators
Used to assign values to variables:
| Operator | Direction | Example | Meaning |
|---|---|---|---|
<- | Left assignment | x <- 10 | Assign 10 to x |
-> | Right assignment | 20 -> y | Assign 20 to y |
= | Equal assignment | z = 30 | Assign 30 to z |
🔔 Best practice: Use <- for general assignment in R scripts.
📈 5. Miscellaneous/Special Operators
: – Sequence Operator
1:5 # Output: 1 2 3 4 5
%in% – Membership Test
3 %in% c(1, 2, 3, 4) # TRUE
%*% – Matrix Multiplication
A <- matrix(c(1,2,3,4), nrow=2)
B <- matrix(c(2,0,1,2), nrow=2)
A %*% B
💡 Combining Operators in Expressions
x <- 5
y <- 3
result <- (x + y) > 7 & x != y
print(result) # TRUE
R evaluates arithmetic first, then relational, then logical.
📌 Summary – Recap & Next Steps
Operators are essential to perform calculations, make decisions, and manipulate data in R. Understanding their behavior allows you to write expressive, optimized, and readable code.
🔍 Key Takeaways:
- Arithmetic operators handle basic math:
+,-,*,/,^ - Comparison operators return logical results:
==,!=,<,> - Logical operators:
&,|,!help control flow and filtering - Assign values using
<-,=, or->(use<-as preferred) - Use special operators like
%in%,:and%*%for sequence, membership, and matrices
⚙️ Real-World Relevance:
Operators are crucial for building models, analyzing datasets, filtering records, and controlling program logic in R-based analytics, machine learning, and reporting.
❓ FAQs – Operators in R
❓ What is the difference between <- and = in R?
✅ Both assign values, but <- is preferred in R scripts for consistency and clarity.
❓ How do I compare two vectors element-wise?
✅ Use ==, !=, <, etc. to get a logical vector:
c(1, 2, 3) == c(1, 2, 0) # TRUE TRUE FALSE
❓ What does : do in R?
✅ Creates a sequence of integers:
1:4 # 1 2 3 4
❓ How to test if a value exists in a vector?
✅ Use %in%:
"apple" %in% c("apple", "banana") # TRUE
❓ What is the purpose of %*% in R?
✅ Performs matrix multiplication:
A %*% B
Share Now :
