R Operator
R Operators
Tutorial Name: Codes With Pankaj Website: www.codeswithpankaj.com
Table of Contents
Introduction to R Operators
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Miscellaneous Operators
Sequence Operator
Membership Operator
Identity Operator
1. Introduction to R Operators
In R, operators are symbols that perform specific operations on one or more operands. Operators are used in mathematical calculations, comparisons, and logical evaluations. R supports various types of operators, including arithmetic, relational, logical, and assignment operators.
2. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division.
List of Arithmetic Operators:
+
: Addition-
: Subtraction*
: Multiplication/
: Division^
: Exponentiation%%
: Modulus (remainder of division)%/%
: Integer division
Example:
3. Relational Operators
Relational operators are used to compare two values. The result of a relational operation is a logical value (TRUE
or FALSE
).
List of Relational Operators:
==
: Equal to!=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to
Example:
4. Logical Operators
Logical operators are used to combine multiple conditions or to negate a condition.
List of Logical Operators:
&
: Element-wise logical AND|
: Element-wise logical OR!
: Logical NOT&&
: Logical AND (used for single logical comparison)||
: Logical OR (used for single logical comparison)
Example:
5. Assignment Operators
Assignment operators are used to assign values to variables. In R, you can use either <-
, =
, or ->
for assignment.
List of Assignment Operators:
<-
: Assigns value from right to left->
: Assigns value from left to right=
: Assigns value from right to left
Example:
6. Miscellaneous Operators
6.1 Sequence Operator
The colon :
operator is used to create a sequence of numbers.
Example:
6.2 Membership Operator
The %in%
operator is used to check if an element belongs to a vector.
Example:
6.3 Identity Operator
The identical()
function is used to check if two objects are exactly the same.
Example:
Conclusion
Operators are essential in R programming, enabling you to perform a wide range of operations, from basic arithmetic to logical comparisons. Understanding these operators is key to manipulating data and writing effective R scripts.
For more tutorials and resources, visit Codes With Pankaj at www.codeswithpankaj.com.
Last updated