In this tutorial you will learn about the R Operators and its application with practical example.
R Operators
An operator is a special symbol that is used to carry out some specific operation on its operand. In R Programming, we have rich set of built in operators to carry out different type of operations. There are operators for assignment, arithmetic operations, logical operations and comparison operations etc. In R, operators can be used with many types of variables or constants, but some of the operators are restricted to work on specific data types. Most operators are binary, meaning they take two operands, but a few are unary and only take one operand.
Type of operators in R Programming
R supports the following types of operators –
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
R Arithmetic Operators
Arithmetic Operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, modulus, exponent, etc.
Let variable a holds 2 and variable b holds 5, then −
Operator | Name | Description | Example |
---|---|---|---|
+ |
Addition |
Addition of given operands |
a+b returns 7 |
- |
Subtraction |
Subtraction of second operand from first |
a-b returns -3 |
* |
Multiply |
Multiplication of given operands |
a*b returns 10 |
/ |
Division |
Returns Quotient after division |
b/a returns 2.5 |
%/% |
Integer Division |
Returns Integer Quotient after division |
b%/%a returns 2 |
%% |
Modulus |
Returns Remainder after division |
b%%a returns 1 |
^ |
Exponent |
Raised to the exponent |
b^a returns 25 |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
> a <- 2 > b <- 5 > a+b [1] 7 > a-b [1] -3 > a*b [1] 10 > b/a [1] 2.5 > b%/%a [1] 2 > b%%a [1] 1 > b^a [1] 25 |
R Assignment Operators
Assignment operators are used to assign value to a variable, you can assign a variable value or the result of an arithmetical expression.
Operator | Description |
---|---|
<-, <<-, = | Leftwards assignment |
->, ->> | Rightwards assignment |
here, <- and = are commonly used for assigning values, they can also be used interchangeably.
Example:-
1 2 3 4 5 6 7 8 9 |
> n <- 10 > n [1] 10 > n = 15 > n [1] 15 > 20 -> n > n [1] 20 |
R Relational Operators
Relational Operators are used evaluate a comparison between two operands. The result of a relational operation is a Boolean value that can only be true or false. Relational Operators are also referred as Comparison operators.
Operator | Description |
---|---|
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
== | Equal to |
!= | Not equal to |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
> a <- 2 > b <-5 > a<b [1] TRUE > a>b [1] FALSE > a<=b [1] TRUE > b>=10 [1] FALSE > b == 12 [1] FALSE > b == 10 [1] FALSE > b == 5 [1] TRUE > a != 5 [1] TRUE > a != 2 [1] FALSE |
R Logical Operators
Logical operators are used to combine expressions with conditional statements using logical (AND,OR,NOT) operators, which results in true or false.
Operator | Description |
---|---|
! | Logical NOT |
& | Element-wise logical AND |
&& | Logical AND |
| | Element-wise logical OR |
|| | Logical OR |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 |
> x <- c(TRUE,FALSE,0,6) > y <- c(FALSE,TRUE,FALSE,TRUE) > !x [1] FALSE TRUE TRUE FALSE > x&y [1] FALSE FALSE FALSE TRUE > x&&y [1] FALSE > x|y [1] TRUE TRUE FALSE TRUE > x||y [1] TRUE |