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

Scatter Plot in R Programming

A Scatter plot is a type of dispersion graph built to represent the various data points of variables. A Scatter plot is also known as a scatter graph or scattergram.  A scatter plot is used...

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.

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.

How to create Histograms in R

R Histograms A Histogram is the graphical representation of the distribution of numeric data. It takes only one numeric variable as input. The variable is cut into several bars (also called...

2 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.

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.

Introduction to R Programming

What is R Programming? R is a programming language which provides an environment for software used for statistical analysis, graphics representation and reporting. It possesses an extensive catalog of...

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 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 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.

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.

Normal Distribution in R Programming

In statistics, the normal distribution is a type of probability function. Normal distribution tells the user about the distribution of data values in the dataset. It is a very important...

3 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.

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.

Analysis of Covariance in R Programming

Analysis of Covariance can also be named ANCOVA. We know that we use the concept of regression analysis for creating models which can explain the effect of the variation in...

2 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.

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.

R JSON File

JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data. JSON file is often used when data is sent from a server to a...

2 minutes read.

R Date and Time

R provides very large range of capabilities to deal with times and dates. Generally, dates are internally stored as integers, and depending on the operations we will need, we could...

3 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.