Operators in R
Operators in R
Introduction
Operators are special symbols used to perform operations on variables, constants, and expressions. They specify the type of computation to be carried out and produce a result. R provides several types of operators for arithmetic calculations, comparisons, logical operations, assignments, and data manipulation.
The various operators available in R are:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Miscellaneous Operators
- Special Operators
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | 5+3 |
- | Subtraction | 5-3 |
* | Multiplication | 5*3 |
/ | Division | 10/2 |
^ | Exponentiation | 2^3 |
%% | Modulus (Remainder) | 10%%3 |
%/% | Integer Division | 10%/%3 |
Example
a <- 10
b <- 3
print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(a^b)
print(a%%b)
print(a%/%b)
Output
[1] 13
[1] 7
[1] 30
[1] 3.333333
[1] 1000
[1] 1
[1] 3
2. Relational Operators
Relational operators compare two values and return either TRUE or FALSE.
| Operator | Description |
|---|---|
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
== | Equal to |
!= | Not equal to |
Example
x <- 10
y <- 20
print(x < y)
print(x > y)
print(x <= y)
print(x >= y)
print(x == y)
print(x != y)
Output
[1] TRUE
[1] FALSE
[1] TRUE
[1] FALSE
[1] FALSE
[1] TRUE
3. Logical Operators
Logical operators combine or negate logical expressions.
Element-wise Logical Operators
| Operator | Description |
|---|---|
& | AND |
| ` | ` |
! | NOT |
Example
x <- TRUE
y <- FALSE
print(x & y)
print(x | y)
print(!x)
Output
[1] FALSE
[1] TRUE
[1] FALSE
Short-Circuit Logical Operators
| Operator | Description |
|---|---|
&& | Short-circuit AND |
| ` |
These operators evaluate only the first element of vectors.
Example
a <- c(TRUE, FALSE)
b <- c(TRUE, TRUE)
print(a && b)
print(a || b)
Output
[1] TRUE
[1] TRUE
4. Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Description |
|---|---|
<- | Left assignment |
= | Simple assignment |
-> | Right assignment |
<<- | Global assignment |
->> | Global right assignment |
Example
x <- 10
y = 20
30 -> z
print(x)
print(y)
print(z)
Output
[1] 10
[1] 20
[1] 30
5. Miscellaneous Operators
(a) Colon Operator (:)
Creates a sequence of numbers.
Example
x <- 1:5
print(x)
Output
[1] 1 2 3 4 5
(b) Membership Operator (%in%)
Checks whether elements belong to a vector.
Example
x <- c(10,20,30,40)
print(20 %in% x)
print(50 %in% x)
Output
[1] TRUE
[1] FALSE
(c) Matrix Multiplication Operator (%*%)
Performs matrix multiplication.
Example
A <- matrix(c(1,2,3,4), nrow=2)
B <- matrix(c(5,6,7,8), nrow=2)
print(A %*% B)
Output
[,1] [,2]
[1,] 23 31
[2,] 34 46
6. Special Operators
Sequence Operator (seq())
Generates sequences.
seq(1,10,2)
Output:
[1] 1 3 5 7 9
Replication Operator (rep())
Repeats elements multiple times.
rep(5,4)
Output:
[1] 5 5 5 5
Vectorized Operations in R
One important feature of R is that operators work element-wise on vectors.
Example
x <- c(1,2,3)
y <- c(4,5,6)
print(x+y)
print(x*y)
Output
[1] 5 7 9
[1] 4 10 18
Operator Precedence in R
The order of evaluation of operators is called operator precedence.
| Precedence | Operators |
|---|---|
| Highest | () |
^ | |
- (unary) | |
*, /, %%, %/% | |
+, - | |
<, <=, >, >=, ==, != | |
! | |
&, && | |
| ` | |
<-, = | |
| Lowest | -> |
Example
x <- 2 + 3 * 4
print(x)
Output
[1] 14
because multiplication has higher precedence than addition.
Summary of Operators in R
| Category | Operators |
|---|---|
| Arithmetic | +, -, *, /, ^, %%, %/% |
| Relational | <, >, <=, >=, ==, != |
| Logical | &, &&, ` |
| Assignment | <-, =, ->, <<-, ->> |
| Sequence | : |
| Membership | %in% |
| Matrix Multiplication | %*% |
Conclusion
Operators are fundamental building blocks in R that enable arithmetic computations, comparisons, logical decisions, assignments, and data manipulations. R supports a wide variety of operators, many of which are vectorized, allowing efficient processing of vectors and matrices. Understanding these operators is essential for developing R programs and performing data analysis effectively.
Comments
Post a Comment