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

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.

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

Measures of Central Tendency in R Programming

Central Tendency or CT is one of the features of descriptive statistics. The Central tendency will let us know how the various groups of data are clustered around the central...

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

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.

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.

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.

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.

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 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 Switch Statement

It is an alternate way to a nested if…else statement. A switch statement permits an expression to be tested against a list of case values. In short, we can say...

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

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.

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