Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

R Operators


Operators

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Example

10 + 5
Try it Yourself »

R divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Miscellaneous operators

R Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

Operator Name Example Try it
+ Addition x + y Try it »
- Subtraction x - y Try it »
* Multiplication x * y Try it »
/ Division x / y Try it »
^ Exponent x ^ y Try it »
%% Modulus (Remainder from division) x %% y Try it »
%/% Integer Division x%/%y Try it »

R Assignment Operators

Assignment operators are used to assign values to variables:

Example

my_var <- 3

my_var <<- 3

3 -> my_var

3 ->> my_var

my_var # print my_var
Try it Yourself »

Note: <<- is a global assigner. You will learn more about this in the Global Variable chapter.

It is also possible to turn the direction of the assignment operator.

x <- 3 is equal to 3 -> x



R Comparison Operators

Comparison operators are used to compare two values:

Operator Name Example Try it
== Equal x == y Try it »
!= Not equal x != y Try it »
> Greater than x > y Try it »
< Less than x < y Try it »
>= Greater than or equal to x >= y Try it »
<= Less than or equal to x <= y Try it »

R Logical Operators

Logical operators are used to combine conditional statements:

Operator Description
& Element-wise Logical AND operator. Returns TRUE if both elements are TRUE
&& Logical AND operator - Returns TRUE if both statements are TRUE
| Elementwise- Logical OR operator. Returns TRUE if one of the statements is TRUE
|| Logical OR operator. Returns TRUE if one of the statements is TRUE
! Logical NOT - Returns FALSE if statement is TRUE

R Miscellaneous Operators

Miscellaneous operators are used to manipulate data:

Operator Description Example
: Creates a series of numbers in a sequence x <- 1:10
%in% Find out if an element belongs to a vector x %in% y
%*% Matrix Multiplication x <- Matrix1 %*% Matrix2

Note: You will learn more about Matrix multiplication and matrices in a later chapter.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.