R operators are symbols that processes computations or operations for variables and values.
There are classified into several categories
Used for basic mathematical calculations.
Operator
Description
Example
+
Addition
5 + 3
-
Subtraction
5 - 3
*
Multiplication
5 * 3
/
Division
5 / 3
%/%
Integer Division
5 %/% 3
%%
Modulus (remainder)
5 %% 3
^
Exponentiation
5^3
# Addition
result_add <- 5 + 3 # result_add is 8
result_add
# Subtraction
result_add <- 5 - 3 # result_sub is 2
result_add
# Multiplication
result_mult <- 5 * 3 # result_mult is 15
result_mult
# Division
result_div <- 5 / 3 # result_div is 1.666667
result_div
# Integer Division
result_int_div <- 5 %/% 3 # result_int_div is 1
result_int_div
# Modulus
result_mod <- 5 %% 3 # result_mod is 2
result_mod
# Exponentiation
result_exp <- 5^3 # result_exp is 125
result_exp
Used for comparisons, and they return a boolean result (1 for true, 0 for false).
Operator
Description
Example
==
Equal to
5 == 3
!=
Not equal to
5 != 3
>
Greater than
5 > 3
<
Less than
5 < 3
>=
Greater than or equal to
5 >= 3
<=
Less than or equal to
5 <= 3
# Equal to
result_equal <- 5 == 3 # result_equal is FALSE
# Not equal to
result_not_equal <- 5 != 3 # result_not_equal is TRUE
# Greater than
result_greater_than <- 5 > 3 # result_greater_than is TRUE
# Less than
result_less_than <- 5 < 3 # result_less_than is FALSE
# Greater than or equal to
result_greater_equal <- 5 >= 3 # result_greater_equal is TRUE
# Less than or equal to
result_less_equal <- 5 <= 3 # result_less_equal is FALSE
Used to perform logical operations, often in conditional statements.
Operator
Description
Example
&
Logical AND
TRUE & FALSE
|
Logical OR
TRUE | FALSE
!
Logical NOT
!TRUE
# Logical AND
result_and <- TRUE & FALSE # result_and is FALSE
# Logical OR
result_or <- TRUE | FALSE # result_or is TRUE
# Logical NOT
result_not <- !TRUE # result_not is FALSE
Some Operators used in R
Operator
Description
Example
%in%
Tests if elements are in a vector
2 %in% c(1, 2, 3)
%>%
Pipe operator (used in magrittr package)
data %>% summary()
%*%
Used for matrix multiplication
matrix1 %*% matrix2
:
Creates a sequence
1:5
?
Provides help for a function or topic
?mean
library(magrittr)
# Example: Applying summary() to a data frame
data <- data.frame(x = rnorm(100), y = rnorm(100))
result <- data %>% summary()
is_in <- 2 %in% c(1, 2, 3)
is_in
?mean
sequence <- 1:5
sequence
matrix1 <- matrix(c(1, 2, 3, 4), nrow = 2)
matrix2 <- matrix(c(5, 6, 7, 8), nrow = 2)
result_matrix <- matrix1 %*% matrix2
print(result_matrix)
The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.