R Operators
A symbol that tells the compiler to perform specific mathematical or logical operations is called operator.
R language supports mainly 5 different types of operators, which are listed below:
- Arithmetic operators: These types of operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, etc.
Operator |
Description |
Example |
+ |
Addition |
> x =10 > y = 20 > x+y Output: [1] 30 |
- |
Subtraction |
> x =20 > y = 10 > x-y Output: [1] 10 |
* |
Multiplication |
> x = 20 > y = 10 > x*y Output: [1] 200 |
/ |
Division |
> x = 20 > y = 10 > y/x Output: [1] 0.5 |
%% |
Modulus(Remainder from division) |
> x = 20 > y = 10 > x%%y Output: [1] 0 |
%/% |
Integer Division |
> x = 20 > y = 10 > y%/%x Output: [1] 0 |
^ or ** |
Exponent |
> x = 5 > y = 2 > x^y Output: [1] 25 |
- Relational Operators: Relational operators define the relation between two entities.
Operator |
Description |
Example |
> |
Greater than |
> x = 10 > y = 20 > x>y Output: [1] FALSE |
< |
Less than |
> x = 10 > y = 20 > x<y Output: [1] TRUE |
<= |
Less than or equal to |
> x = 10 > y = 20 > x<=y Output: [1] TRUE |
>= |
Greater than or equal to |
> x = 10 > y = 20 > x>=y Output: [1] FALSE |
== |
Equal to |
> x = 10 > y = 20 > x==y Output: [1] FALSE |
!= |
Not equal to |
> x = 10 > y = 20 > x!=y Output: [1] TRUE |
- Logical Operators: Logical operators are used to compare the two entities and are typically used with Boolean values.
Operator |
Description |
Example |
! |
Logical NOT |
> x<- c(TRUE, FALSE, 0, 6) > !x Output: [1] FALSE TRUE TRUE FALSE |
& |
Element wise logical AND (element-wise comparison between vectors and return a logical vector) |
> x <- c(TRUE, FALSE, 0, 6) > y <- c(FALSE, TRUE, FALSE, TRUE) > x&y Output: [1] FALSE FALSE FALSE TRUE |
&& |
Logical AND (compare just the first elements of each vector and return a single logical value) |
> x <- c(TRUE, FALSE, 0, 6) > y <- c(FALSE, TRUE, FALSE, TRUE) > x&&y Output: [1] FALSE |
| |
Element-wise logical OR (element-wise comparison between vectors and return a logical vector) |
> x <- c(TRUE, FALSE, 0, 6) > y <- c(FALSE, TRUE, FALSE, TRUE) > x|y Output: [1] TRUE TRUE FALSE TRUE |
|| |
Logical OR (compare just the first elements of each vector and return a single logical value) |
> x <- c(TRUE, FALSE, 0, 6) > y <- c(FALSE, TRUE, FALSE, TRUE) > x||y Output: [1] TRUE |
- Assignment operators: It is used to assign values.
Operator |
Description |
Example |
<- or <<- or = |
Left assignment |
> x <- 10 > y = 20 > z <<- 30 > x > y > z Output: [1] 10 [1] 20 [1] 30 |
-> Or ->> |
Right assignment |
> 10 -> x > 20 ->> y > x > y Output: [1] 10 [1] 20 |
- Miscellaneous operators:
Operator |
Description |
Example |
: |
Colon used to create a series of numbers in sequence |
> v <- 10:15 > v Output: 10 11 12 13 14 15 |
%in% |
Used to check if an element belongs to a vector |
> a = 10 > b = 100 > c = 10:15 > a %in% c > b %in% c Output: TRUE FALSE |
Reference: https://www.javatpoint.com/r-operators https://www.datamentor.io/r-programming/operator/