R Operators
Operators in R are symbols or keywords used to perform operations on variables and values. They are categorized into arithmetic, relational, logical, assignment, and some special operators.
Key Topics
1. Arithmetic Operators
Arithmetic operators are used for mathematical calculations, such as addition, subtraction, multiplication, division, modulus, and exponentiation.
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 10 - 4 = 6 |
* | Multiplication | 7 * 2 = 14 |
/ | Division | 20 / 4 = 5 |
%% | Modulus (remainder) | 5 %% 2 = 1 |
^ or ** | Exponentiation | 2 ^ 3 = 8 |
# Using arithmetic operators
addition <- 5 + 3
subtraction <- 10 - 4
multiplication <- 7 * 2
division <- 20 / 4
modulus <- 5 %% 2
exponentiation <- 2 ^ 3
print(addition)
print(subtraction)
print(multiplication)
print(division)
print(modulus)
print(exponentiation)
Output:
[1] 6
[1] 14
[1] 5
[1] 1
[1] 8
Code Explanation: Arithmetic operators perform basic math operations: addition, subtraction, multiplication, division, modulus (remainder of division), and exponentiation.
2. Relational Operators
Relational operators compare two values and return a Boolean result: TRUE
or FALSE
. They are used in conditions and loops.
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 5 returns TRUE |
!= | Not equal to | 5 != 3 returns TRUE |
> | Greater than | 7 > 5 returns TRUE |
< | Less than | 3 < 8 returns TRUE |
>= | Greater than or equal to | 5 >= 5 returns TRUE |
<= | Less than or equal to | 4 <= 6 returns TRUE |
# Using relational operators
x <- 5
y <- 10
is_equal <- (x == y)
is_not_equal <- (x != y)
is_greater <- (x > y)
is_less <- (x < y)
is_greater_equal <- (x >= y)
is_less_equal <- (x <= y)
print(is_equal)
print(is_not_equal)
print(is_greater)
print(is_less)
print(is_greater_equal)
print(is_less_equal)
Output:
[1] TRUE
[1] FALSE
[1] TRUE
[1] FALSE
[1] TRUE
Code Explanation: Relational operators compare values of x
and y
, returning TRUE
or FALSE
based on the comparison.
3. Logical Operators
Logical operators are used to combine or negate Boolean values. They include AND, OR, and NOT operations and are crucial for controlling program flow.
Operator | Description | Example |
---|---|---|
& | AND | TRUE & FALSE returns FALSE |
| | OR | TRUE | FALSE returns TRUE |
! | NOT | !TRUE returns FALSE |
# Using logical operators
result_and <- TRUE & FALSE
result_or <- TRUE | FALSE
result_not <- !TRUE
print(result_and)
print(result_or)
print(result_not)
Output:
[1] TRUE
[1] FALSE
Code Explanation: The logical operators perform operations on Boolean values: &
for AND, |
for OR, and !
for NOT.
4. Assignment Operators
Assignment operators are used to assign values to variables. They include leftward, rightward, and equal assignment.
Operator | Description | Example |
---|---|---|
<- | Leftward assignment | x <- 5 |
= | Assignment | y = 10 |
>- | Rightward assignment | 5 -> z |
# Using assignment operators
x <- 5
y = 10
5 -> z
print(x)
print(y)
print(z)
Output:
[1] 10
[1] 5
Code Explanation: The assignment operators assign values to variables. <-
and =
are used for leftward assignment, while ->
assigns a value rightward.
5. Special Operators
Special operators in R include the colon operator and the membership operator. These operators have specific uses in R programming.
Operator | Description | Example |
---|---|---|
: | Creates a sequence of numbers | 1:5 returns 1, 2, 3, 4, 5 |
%in% | Checks if an element belongs to a vector | 3 %in% c(1, 2, 3) returns TRUE |
# Using special operators
sequence <- 1:5
is_member <- 3 %in% c(1, 2, 3)
print(sequence)
print(is_member)
Output:
[1] TRUE
Code Explanation: The colon operator :
creates a sequence of numbers, while the membership operator %in%
checks if an element is in a vector.
Key Takeaways
- R supports arithmetic, relational, logical, assignment, and special operators for various operations.
- Arithmetic operators are used for basic mathematical calculations.
- Relational operators compare values and return Boolean results.
- Logical operators are used to combine or negate Boolean values.
- Assignment operators assign values to variables.
- Special operators include the colon for sequences and
%in%
for membership checks.