x <- 227
y <- 639
7 The primary types of operators in R
7.1 Questions
What are the primary operator types in R?
How can one use the various operator types in data analysis?
How do arithmetic, comparison, and logical operators work in R?
When should I use assignment operators versus comparison operators?
7.2 Learning Objectives
Identify the primary operator types in R.
Learn how to use various operators for data manipulation.
Utilize arithmetic operators for efficient numerical calculations in your R scripts.
Apply comparison operators to evaluate conditions and filter data based on logical comparisons.
Employ logical operators to combine multiple conditions and control the flow of your R code.
7.3 Lesson Content
7.3.1 Introduction
R has many types of operators that can perform different tasks. Here we will focus on 5 major types of operators. The major types of operators are:
- Arithmetic,
- Relational,
- Logical,
- Assignment, and
- Miscellaneous.
7.3.2 Arithmetic Operators
Arithmetic operators are used to perform mathematical operations. These operators were reviewed in the previous lesson on “Basic arithmetic, arithmetic operators, and variables” (sec-arithmetic-variables)
7.3.3 Relational Operators
Relational operators are used to find the relationship between 2 variables and compare objects. The output of these comparisons is Boolean (TRUE or FALSE). The table below describes the most common relational operators.
Relational Operator | Description |
---|---|
< |
Less than |
> |
Greater than |
<= |
Less than or equal to |
>= |
Greater than or equal to |
== |
Equal to |
!= |
Not Equal to |
7.3.3.1 Assign values to variables
- Less than
x < y
- Greater than
x > y
- Less than or equal to
x <= 300
- Greater than or equal to
y >= 700
- Equal to
y == 639
- Not Equal to
x != 227
7.3.4 Logical Operators
Logical operators are used to specify multiple conditions between objects. Logical operators work with basic data types such as logical, numeric, and complex data types. These operators generally return TRUE
or FALSE
values. Numbers greater that 1 are TRUE
and 0 equals FALSE
. The table below describes the most common logical operators.
Logical Operator | Description |
---|---|
! |
Logical NOT |
| |
Element-wise logical OR |
& |
Element-wise logical AND |
7.3.4.1 Assign vectors to variables
NOTE: Full discussion in the chapter on “Vectors” (sec-vectors)
- Logical NOT
!vector_1
!vector_2
- Element-wise Logical OR
vector_1 | vector_2
- Element-wise Logical AND
vector_1 & vector_2
7.3.5 Assignment Operators
These operators assign values to variables. A more comprehensive review can be obtained in the lesson on “Basic arithmetic, arithmetic operators, and variables” (sec-arithmetic-variables)
7.3.6 Miscellaneous Operators
These are helpful operators for working in that can perform various functions. A few common miscellaneous operators are described below.
Miscellaneous Operator | Description |
---|---|
%*% |
Matrix multiplication (to be discussed in subsequent chapters) |
%in% |
Does an element belong to a vector |
: |
Generate a sequence |
- Sequence
a <- 1:8
a
b <- 4:10
b
- Element in a vector
a %in% b
9 %in% b
9 %in% a
7.4 Exercises
List the basic operator types in R and provide an example for each.
What operators allow you to subset vectors, lists, and data frames in R?
Explain how the modulus operator (
%/%
) differs from the regular division operator (/
).What are the membership operators
%in%
and!%in%
used for in R?Assign 10 to x using
<-
. Then compare ifx == 10
.Calculate
3^4
using arithmetic operators.Compare
3 > 5
. Is the result TRUE or FALSE?When would you use the sequence operator (
:
) in R? Give an example.
7.5 Summary
The focus of this chapter has been the five major types of operators that can be used in R/RStudio. The operators allow one to perform numerous tasks including basic arithmetic including basic arithmetic, comparisons, subsetting, matrix multiplication, and vector generation. In the next chapter, we will discuss the available data types in R and how to utilize/handle them.