HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RVariablesData TypesOperatorsCharacters (character)VectorsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ Character
📚 Vector
🔀 Condition & Loop
🧩 Function
R›Operators

Operators

Math (+ - * / %/% %% ^), comparison (== != < >) and logic (& | !).

Simple enough for a 5-year-old to understand

Math operators

/ gives decimal; integer division is %/%, remainder is %%, and exponentiation is with ^.

Example code
1
cat(7 + 3, "\n", sep="")
10
Output / Input10
2
cat(7 / 2, "\n", sep="")
3.5
Output / Input3.5
3
cat(7 %/% 2, "\n", sep="")
3 (integer division)
Output / Input3 (integer division)
4
cat(7 %% 2, "\n", sep="")
1 (remainder)
Output / Input1 (remainder)
5
cat(2 ^ 10, "\n", sep="")
1024 (power)
Output / Input1024 (power)
Output / Input
💡 Hint: Integer division %/%, remainder %%, exponent ^.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of 23 divided by 4.

🎯 Expected output: 3

Loading editor…

About this topic

R's operators are grouped as arithmetic, comparison, and logical. The most important difference from other languages is that operators work element-wise on vectors: when you add two vectors, corresponding elements are added.

This vectorized behavior makes R code shorter and faster because you apply the operation directly to the vector instead of writing a loop. However, it has a side effect: with two vectors of different lengths, R recycles the shorter one and often won't even give a warning.

There is a binary distinction in logical operators. The single-character form applies to every element of the vector; the double-character form looks only at the first element and short-circuits. The double-character form is used in conditional expressions.

What you will learn

  • Arithmetic operators and integer division
  • Comparison operators
  • Vectorized and scalar logical operators
  • The recycling rule
  • Operator precedence

Who is this for?

  • Those learning vector operations
  • Those getting unexpected results when writing conditions
  • Those coming from other languages expecting single-element operations

When to use?

In column-based calculations, vectorized operators are both much shorter and noticeably faster than loops.

When evaluating a single condition, double-character logical operators should be used; the vectorized form produces an error here.

Common mistakes

Wrong

a <- c(1, 2, 3, 4)
b <- c(10, 20)
print(a + b)

Since the lengths are different, R recycles the short vector and the result becomes 11, 22, 13, 24. Since the length is an exact multiple, it doesn't even give a warning. This recycling rule is powerful when used consciously, but it is the stealthiest way to produce incorrect results unknowingly.

Correct

a <- c(1, 2, 3, 4)
b <- c(10, 20, 30, 40)
print(a + b)

Wrong

x <- 5
if (x > 0 & x < 10) print("in range")

The single-character logical operator is vectorized; although the result might look correct when working with a single value, using it in conditions is not recommended, and it behaves unexpectedly if a vector is passed. The short-circuiting double-character form should be used in conditional expressions.

Correct

x <- 5
if (x > 0 && x < 10) print("in range")

Wrong

print(7 / 2)
print(7 %% 2)
print(7 / 2 == 3)

In R, a single forward slash always performs floating-point division; 7 divided by 2 results in 3.5, not 3. Beginners coming from other languages expect integer division and the comparison turns out wrong. There is a separate operator for integer division.

Correct

print(7 %/% 2)
print(7 %% 2)
print(7 %/% 2 == 3)

Frequently asked questions

Why does the recycling rule exist?

To make operations like multiplying all elements of a vector by a single number natural. In that case, the short vector has a single element and its recycling is the expected behavior. The problem arises when the lengths of two long vectors are accidentally different.

How do I catch a length mismatch?

If the length is not an exact multiple, R gives a warning, but if it is a multiple, it remains silent. The safe way is to compare lengths with length before the operation or to write your assumption into the code with stopifnot, so the error doesn't go unnoticed.

Which operator is used for exponentiation?

The caret sign is used; two asterisks also work, but the caret is the standard one. There are sqrt functions for square roots and log functions for natural logarithms, and these also work vectorized.

Summary

Operators work element-wise on vectors.

The short vector can be silently recycled.

A single forward slash performs floating-point division.

There are separate operators for integer division and modulo.

Logical operators that short-circuit are used in conditions.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code