R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Operators in R are symbols that perform specific operations on one or more operands. R has a wide range of operators to perform tasks ranging from arithmetic to logical comparisons to member selection.
In this tutorial, we will cover:
These operators are used to perform arithmetic calculations.
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 3 + 4 | 7 |
- | Subtraction | 7 - 4 | 3 |
* | Multiplication | 3 * 4 | 12 |
/ | Division | 8 / 2 | 4 |
%% | Modulus | 5 %% 3 | 2 |
%/% | Integer Division | 5 %/% 3 | 1 |
^ | Exponentiation | 3^2 | 9 |
These operators are used to compare two values.
Operator | Description | Example | Result |
---|---|---|---|
== | Equal to | 5 == 3 | FALSE |
!= | Not equal to | 5 != 3 | TRUE |
> | Greater than | 5 > 3 | TRUE |
< | Less than | 5 < 3 | FALSE |
>= | Greater than or equal to | 5 >= 5 | TRUE |
<= | Less than or equal to | 5 <= 7 | TRUE |
Used to perform logical operations.
Operator | Description | Example | Result |
---|---|---|---|
& | Element-wise AND | c(TRUE, FALSE) & c(TRUE, TRUE) | c(TRUE, FALSE) |
` | ` | Element-wise OR | c(TRUE, FALSE) | c(FALSE, TRUE) |
! | NOT | !c(TRUE, FALSE) | c(FALSE, TRUE) |
&& | And (returns single TRUE or FALSE ) | TRUE && FALSE | FALSE |
|| | Or (returns single TRUE or FALSE ) | TRUE || FALSE | TRUE |
Used to assign values.
Operator | Description | Example |
---|---|---|
<- | Assigns value on the right to the left | x <- 5 |
-> | Assigns value on the left to the right | 5 -> x |
= | Assigns value (mostly used in function arguments) | f(x = 5) |
Operator | Description | Example | Result |
---|---|---|---|
: | Creates a sequence | 3:6 | 3 4 5 6 |
%in% | Checks if left is a member of right | 3 %in% c(1, 2, 3, 4) | TRUE |
Operators in R are powerful and allow for flexible manipulation and evaluation of data. Understanding them is crucial as they form the foundation for almost every operation and evaluation in R programming.
Arithmetic operators in R programming:
# Example a <- 5 b <- 3 addition_result <- a + b subtraction_result <- a - b multiplication_result <- a * b division_result <- a / b
Comparison operators in R:
# Example x <- 10 y <- 15 greater_than <- x > y less_than_equal <- x <= y not_equal <- x != y
Logical operators in R:
# Example p <- TRUE q <- FALSE and_result <- p & q or_result <- p | q not_result <- !p
Assignment operators in R:
# Example x <- 10 y <<- 20 # Assign in global environment z <- c(1, 2, 3)
Bitwise operators in R:
# Example a <- 5 b <- 3 bitwise_and <- a & b bitwise_or <- a | b bitwise_xor <- xor(a, b)
Special operators in R (e.g., %in%, %*%):
# Example vector_check <- 3 %in% c(1, 2, 3, 4) matrix_multiply <- matrix(1:4, nrow = 2) %*% matrix(1:4, ncol = 2)
Vectorized operations and operators in R:
# Example vector_a <- c(1, 2, 3) vector_b <- c(4, 5, 6) elementwise_sum <- vector_a + vector_b
Operator precedence in R:
# Example result <- 2 + 3 * 4
Overloading operators in R programming:
# Example (S3 method for overloading +) setMethod("+", signature = c("MyClass", "MyClass"), function(e1, e2) { # Custom addition logic })