EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials R Programming Tutorial R Operators
 

R Operators

Priya Pedamkar
Article byPriya Pedamkar

Updated May 26, 2023

R Operators

 

 

Introduction to R Operators

R Operators are the features the R language supports for processing certain operations on the operands or participating variables and data values. The Operators in the R language are of the following types: arithmetic, logical, relational, and assignment operators. The operators in the R language are represented through symbols and processed by the R runtime environment. For example, some of the commonly used arrive, maniac operators are addition operators represented by the ‘+’ symbol for adding two vector values in R, and ‘=’ or ‘<- ‘ is the assignment operator used to assign values to the variable. The R operators help to develop standardized program logic.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Operators

R Operators have many built-in operators. We will look at them one by one. Operators in R can be classified into four categories:

1. Arithmetic Operators

These R operators, as their name suggests, perform operations like addition, subtraction, multiplication, division, exponentiation, modulus, and integer division. The symbols are shown below:

  1. (+) Addition Operator – It adds two vectors.
  2. (–) Subtraction Operator – It subtracts the second vector from the first vector.
  3. (*) Multiplication Operator – It multiplies two vectors.
  4. (/) Division Operator – It divides the first vector with the second vector.
  5. (% %) Modulus Operator – It gives the remainder of the first vector with the second.
  6. (%/%) Integer Division Operator – It gives the quotient of the first vector with the second vector.
  7. (^) Exponent Operator – The first vector is exponentiated by the second vector.

A few examples are shown below:

x <- 6

> y <- 17

> x+y

[1] 23

> x-y

[1] -9

> x*y

[1] 102

x <- 5

y <- 16

> y/x

[1] 3.2

> y%/%x

[1] 3

> y%%x

[1] 1

These also work on vectors. Here are a few examples of arithmetic operators being used in vectors.

> x <- c(2,8,3)

> y <- c(6,4,1)

> x+y

[1]  8 12  4

>x-y

[1] -4 4 2

2. Relational Operators

As you know by the name, relation operator means the relationship between two values or compare between two values or two operands. Below is the list of symbols, along with their operations :

  1. (<) Less than Operator – It returns true for elements in the first vector that are less than the corresponding element present in the second vector.
  2. (>) Greater than Operator – It returns true for elements in the first vector greater than the corresponding element present in the second vector.
  3. (< =) Less than or Equal to – As the name suggests, It returns true for elements in the first vector which is less than or equal to the corresponding element present in the second vector.
  4. (> =) Greater than or Equal to – It returns true for elements in the first vector greater than or equal to the corresponding element present in the second vector.
  5. (= =) Equal to – As the name suggests, It returns true for elements in the first vector equal to the corresponding element present in the second vector.
  6. (! =) Not Equal to – It returns true for elements in the first vector that are not equal to the corresponding element present in the second vector.

A few examples are shown below:

> y <- 16

> x<y

[1] TRUE

> x>y

[1] FALSE

> x<=5

[1] TRUE

> y>=20

[1] FALSE

> y == 16

[1] TRUE

> x != 5

[1] FALSE

These also work on vectors. Here are a few examples of arithmetic operators being used in vectors.

> x <- c(2,8,3)

> y <- c(6,4,1)

> x>y

[1] FALSE  TRUE  TRUE

3. Logical Operators

Logical Operators are used for carrying out Boolean operations like AND and OR. Logical vectors are only applicable to vectors of logical type, numeric or complex. The numbers which are more significant than one are true. Below is the list of logical operators and their operations.

  1. & – It is an element-wise Logical AND Operator. It combines each element of the first vector with the corresponding element of the second vector, and based on the result; it returns TRUE or FALSE.
  2. | – It is an element-wise Logical OR Operator. Returns TRUE or FALSE. It can return only one of the two available outputs.
  3. ! – It is Logical, NOT Operator. It works by applying the logical inverse to each element of the vector.
  4. && – It is Logical AND Operator. Returns TRUE only if both the elements from the vectors are TRUE, and it returns FALSE if any one or both of them are false.
  5. || – It is a Logical OR Operator. Unlike the previous vector returns TRUE even if one of them is TRUE. It returns false if both are FALSE.

A few examples are shown below:

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

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

> !x

[1] FALSE  TRUE  TRUE FALSE

> x&y

[1] FALSE FALSE FALSE  TRUE

> x&&y

[1] FALSE

> x|y

[1]  TRUE  TRUE FALSE  TRUE

> x||y

[1] TRUE

4. Assignment Operators

Assignment R operators are very simple; they assign values to vectors, as the name suggests. Below is the list of various operators and operations:

  1. <-,<<-, =: It is called Leftwards assignment operators.
  2. ->, ->>: It is called Rightward assignment operators.

The operators <- and = can be used to assign variables in the same environment, and the operator <<- is generally used in global assignments.

A few examples are shown below:

> x <- 5

> x

[1] 5

> x = 9

> x

[1] 9

> 10 -> x

Advantages Of R Operators

So, now let’s conclude this article with the various advantages that R has:

  • It is free and open-source.
  • It supports a wide variety of extensions, for example, data manipulations, statistical modeling, and graphics.
  • They run in every operating system, like Windows, Unix(such as Linux), and Mac.
  • It easily connects with other languages, such as connecting and reading from a database using the Open Database Connectivity (ODBC) protocol.

Recommended Articles

This has been a guide to R Operators. Here we discussed the Concept, Various Operators with Examples, and Advantages of R. You can also review our other suggested Articles to learn more.

  1. Overview of C Operators
  2. MySQL Functions and Operators
  3. Learn the easy way To Install Swift
  4. PHP Commands | Basic and Advanced
  5. Guide to Types of Assignment Operators in Java

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW