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.

OperatorNameExampleDescription
+Addition5 + 3Adds two numbers together (5 plus 3 equals 8).
-Subtraction5 - 3Subtracts one number from another (5 minus 3 equals 2).
*Multiplication5 * 3Multiplies two numbers (5 times 3 equals 15).
/Division6 / 3Divides one number by another (6 divided by 3 equals 2).
^Exponentiation2 ^ 3Raises 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.

OperatorNameExampleDescription
==Equal to5 == 5Checks if two values are equal (5 is equal to 5).
!=Not equal to5 != 3Checks if two values are not equal (5 is not equal to 3).
>Greater than5 > 3Checks if the first number is greater than the second (5 is greater than 3).
<Less than3 < 5Checks if the first number is less than the second (3 is less than 5).
>=Greater than or equal to5 >= 5Checks if the first number is greater than or equal to the second (5 is greater than or equal to 5).
<=Less than or equal to3 <= 5Checks 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.

OperatorNameExampleDescription
&ANDTRUE & FALSEChecks if both conditions are true (TRUE AND FALSE is FALSE).
``OR`TRUE
!NOT!TRUEReverses 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.

OperatorNameExampleDescription
<-Assignmentx <- 5Assigns the value 5 to the variable x.
=Assignmenty = 10Assigns 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.