R Operators

A symbol that tells the compiler to perform specific mathematical or logical operations is called operator.

R language supports mainly 5 different types of operators, which are listed below:

  1. Arithmetic operators: These types of operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, etc.

Operator

Description

Example

+

Addition

> x =10

> y = 20

> x+y

Output: [1] 30

-

Subtraction

> x =20

> y = 10

> x-y

Output: [1] 10

*

Multiplication

> x = 20

> y = 10

> x*y

Output: [1] 200

/

Division

> x = 20

> y = 10

> y/x

Output: [1] 0.5

%%

Modulus(Remainder from division)

> x = 20

> y = 10

> x%%y

Output: [1] 0

%/%

Integer Division

> x = 20

> y = 10

> y%/%x

Output: [1] 0

^ or **

Exponent

> x = 5

> y = 2

> x^y

Output: [1] 25


  1. Relational Operators: Relational operators define the relation between two entities.

Operator

Description

Example

>

Greater than

> x = 10

> y = 20

> x>y

Output: [1] FALSE

<

Less than

> x = 10

> y = 20

> x<y

Output: [1] TRUE

<=

Less than or equal to

> x = 10

> y = 20

> x<=y

Output: [1] TRUE

>=

Greater than or equal to

> x = 10

> y = 20

> x>=y

Output: [1] FALSE

==

Equal to

> x = 10

> y = 20

> x==y

Output: [1] FALSE

!=

Not equal to

> x = 10

> y = 20

> x!=y

Output: [1] TRUE


  1. Logical Operators: Logical operators are used to compare the two entities and are typically used with Boolean values.

Operator

Description

Example

!

Logical NOT

> x<- c(TRUE, FALSE, 0, 6)

> !x

Output: [1] FALSE TRUE TRUE FALSE

&

Element wise logical AND (element-wise comparison between vectors and return a logical vector)

> x <- c(TRUE, FALSE, 0, 6)

> y <- c(FALSE, TRUE, FALSE, TRUE)

> x&y

Output: [1] FALSE FALSE FALSE TRUE

&&

Logical AND (compare just the first elements of each vector and return a single logical value)

> x <- c(TRUE, FALSE, 0, 6)

> y <- c(FALSE, TRUE, FALSE, TRUE)

> x&&y

Output: [1] FALSE

|

Element-wise logical OR (element-wise comparison between vectors and return a logical vector)

> x <- c(TRUE, FALSE, 0, 6)

> y <- c(FALSE, TRUE, FALSE, TRUE)

> x|y

Output: [1] TRUE TRUE FALSE TRUE

||

Logical OR (compare just the first elements of each vector and return a single logical value)

> x <- c(TRUE, FALSE, 0, 6)

> y <- c(FALSE, TRUE, FALSE, TRUE)

> x||y

Output: [1] TRUE


  1. Assignment operators: It is used to assign values.

Operator

Description

Example

<- or <<- or =

Left assignment

> x <- 10

> y = 20

> z <<- 30

> x

> y

> z

Output:

[1] 10

[1] 20

[1] 30

-> Or ->>

Right assignment

> 10 -> x

> 20 ->> y

> x

> y

Output:

[1] 10

[1] 20


  1. Miscellaneous operators: 

Operator

Description

Example

:

Colon used to create a series of numbers in sequence

> v <- 10:15

> v

Output: 10 11 12 13 14 15

%in%

Used to check if an element belongs to a vector

> a = 10

> b = 100

> c = 10:15

> a %in% c

> b %in% c

Output:

TRUE

FALSE


Reference: https://www.javatpoint.com/r-operators https://www.datamentor.io/r-programming/operator/

Related Topics

Bubble Chart in R Programming

In this article, we will talk about another type of chart in R programming, i.e., the Bubble chart. A bubble chart is a type of data visualization chart that helps display multiple...

4 minutes read.

Decision Tree in R Programming

The decision tree uses the branching method to show every possible output for the specific input. We can draw a decision tree by hand or create it using specialized software...

2 minutes read.

R Data Types

Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are...

4 minutes read.

R Line Graphs

Line graph or line chart is a graph that connects a series of points by drawing segments between them. A line graph represents the relationship between 2 variables. These points...

2 minutes read.

R Basic Data Types

Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are...

4 minutes read.

First R Program

First  Hello World R Program As a convention, our first R program will be the “Hello World!” program. We can run our R program either at R command prompt or we...

3 minutes read.

Poisson Distribution in R Programming

Poisson distribution is a type of distribution that deals with the probability distribution of the data values by taking the mean into consideration. Poisson distribution will estimate the probability value for...

3 minutes read.

Clustering in R Programming

The clustering technique is an unsupervised machine learning in R programming. Before discussing clustering techniques, let's have a look at what unsupervised machine learning is?  Unsupervised learning is training the model with...

3 minutes read.

R Pie Charts

R programming language provides many numbers of libraries to create charts and graphs. A pie-chart is a representation of data as slices of a circle with different colors representing counts...

2 minutes read.

R Functions

In programming, a function is a group of instructions that you want to use repeatedly, because of their complexity, are better self-contained in sub-program and called when needed. Hence, we can...

4 minutes read.

R If Else Statement

If statement It is the most simple decision-making statement. It checks the condition if the condition is true then only it will execute the block of statements written in the ‘if...

2 minutes read.

Binomial Distribution in R Programming

In this article, we will talk about the Binomial distribution in R programming. The binomial distribution is a type of probability distribution. As it is a discrete distribution, it will...

3 minutes read.

R Tutorial

Introduction to R Programming R is nothing but a programming language, also a free software environment. This software used for predictive analysis, statistical analysis, graphical representation, data modeling, and reporting. R...

9 minutes read.

Logistic Regression in R Programming

Logistic Regression is a classification supervised machine learning algorithm in R programming. Logistic Regression can also be termed Binomial Logistic Regression, Binary Logistic Regression, or Logit Model. Logistic Regression is...

4 minutes read.

How to make Boxplots in R

R Boxplots Boxplot is a measure of how well the data is distributed in a data set. It is used to give a summary of one or several numeric variables. The...

3 minutes read.

R Excel Files

Microsoft Excel is the spreadsheet program which stores data in the .xls and .xlsx format. R has the facility to read directly from these files using some excel specific packages....

2 minutes read.

Various types of Charts in R Programming

As we know, R Language is mostly used for analyzing the data and statistics purposes to represent our data graphically & pictorially in R studio. Charts and graphs are plotted in...

4 minutes read.

R Variables, Constants and Reserved Words

R Variables Variables are very similar to an open box, where you can put any value of your wish. We can change the values as well. Variables are used to store...

4 minutes read.

Pie Charts in R Programming

R programming has various libraries to plot various types of charts and graphs. A pie chart is a circular statistical chart or graph, which is divided into slices to represent the numerical...

4 minutes read.

R Inheritance

One the main concept of object oriented programming is inheritance which allows us to define a new class of existing classes. This means that we can derive new classes from...

5 minutes read.