Operators are symbols that allow you to perform different types of operations on data. In simple terms, operators help you manipulate and work with the numbers, text, or other values in your R program.
Let’s break down the main types of operators you will use in R.
1. Arithmetic Operators
These operators allow you to perform basic mathematical operations like addition, subtraction, multiplication, and division.
Operator | Name | Example | Description |
---|---|---|---|
+ | Addition | 5 + 3 | Adds two numbers together (5 plus 3 equals 8). |
- | Subtraction | 5 - 3 | Subtracts one number from another (5 minus 3 equals 2). |
* | Multiplication | 5 * 3 | Multiplies two numbers (5 times 3 equals 15). |
/ | Division | 6 / 3 | Divides one number by another (6 divided by 3 equals 2). |
^ | Exponentiation | 2 ^ 3 | Raises one number to the power of another (2 raised to the power of 3 equals 8). |
2. Relational (Comparison) Operators
These operators are used to compare two values and check if they are equal, greater than, or less than one another. They return either TRUE
or FALSE
.
Operator | Name | Example | Description |
---|---|---|---|
== | Equal to | 5 == 5 | Checks if two values are equal (5 is equal to 5). |
!= | Not equal to | 5 != 3 | Checks if two values are not equal (5 is not equal to 3). |
> | Greater than | 5 > 3 | Checks if the first number is greater than the second (5 is greater than 3). |
< | Less than | 3 < 5 | Checks if the first number is less than the second (3 is less than 5). |
>= | Greater than or equal to | 5 >= 5 | Checks if the first number is greater than or equal to the second (5 is greater than or equal to 5). |
<= | Less than or equal to | 3 <= 5 | Checks if the first number is less than or equal to the second (3 is less than or equal to 5). |
3. Logical Operators
Logical operators are used to perform logical operations. They are often used with conditions to check if multiple statements are true or false.
Operator | Name | Example | Description |
---|---|---|---|
& | AND | TRUE & FALSE | Checks if both conditions are true (TRUE AND FALSE is FALSE). |
` | ` | OR | `TRUE |
! | NOT | !TRUE | Reverses the logical value (NOT TRUE is FALSE). |
4. Assignment Operators
Assignment operators are used to assign values to variables in R. This allows you to store data in variables and use them later in your program.
Operator | Name | Example | Description |
---|---|---|---|
<- | Assignment | x <- 5 | Assigns the value 5 to the variable x . |
= | Assignment | y = 10 | Assigns the value 10 to the variable y . |
Both <-
and =
can be used to assign values to variables, but <-
is more commonly used in R.
5. Miscellaneous Operators
Here are some other useful operators in R that don’t fall into the categories above.
$ Operator
This operator is used to access specific elements in a data frame or list.
Example:
data <- data.frame(name = c("John", "Jane"), age = c(23, 25))
data$name
This would return the name
column from the data frame.
:
(Colon Operator)
The colon operator is used to create sequences of numbers.
Example:
1:5
This will give you the sequence 1, 2, 3, 4, 5
.