R Core Language Concepts
Estimated reading: 3 minutes 46 views

⚙️ 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:

OperatorDescriptionExampleResult
+Addition3 + 25
-Subtraction7 - 34
*Multiplication4 * 28
/Division10 / 25
^ or **Exponentiation2 ^ 38
%%Modulus (remainder)7 %% 31
%/%Integer Division7 %/% 32

🔍 2. Relational (Comparison) Operators

Return TRUE or FALSE by comparing values:

OperatorDescriptionExampleResult
==Equal to5 == 5TRUE
!=Not equal to5 != 3TRUE
>Greater than5 > 2TRUE
<Less than3 < 5TRUE
>=Greater than or equal4 >= 4TRUE
<=Less than or equal3 <= 2FALSE

🔗 3. Logical Operators

Used for combining Boolean values or logical conditions:

OperatorDescriptionExampleResult
&AND (element-wise)TRUE & FALSEFALSE
``OR (element-wise)`TRUE
!NOT!TRUEFALSE
&&AND (first element only)TRUE && FALSEFALSE
``OR (first element only)

🎯 4. Assignment Operators

Used to assign values to variables:

OperatorDirectionExampleMeaning
<-Left assignmentx <- 10Assign 10 to x
->Right assignment20 -> yAssign 20 to y
=Equal assignmentz = 30Assign 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 :

Leave a Reply

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

Share

R – Operators

Or Copy Link

CONTENTS
Scroll to Top