R operators

R operators perform arithmetic, compare values, assign objects to names, and test logical conditions.

A simple example

Trace two arithmetic operators

  1. Start with 7 and 3
  2. Do this Divide into groups of 3
  3. Result 2 groups, 1 left
Integer division counts full groups. The remainder counts the items left over.
Lesson.R
cat(7 %/% 3, 7 %% 3, sep = "\n")
Output
2
1
Notebooks: 3 × 125¢ = 375¢. Delivery: 50¢. Amount to pay: 425¢.
Check a stationery order

A student buys three notebooks at 125 cents each. Add the 50-cent delivery charge once for the whole order. Keeping the arithmetic in cents makes the total easy to check against the receipt.

Types of Operators

  1. Assignment operator
  2. Arithmetic operator
  3. Relational operator
  4. Logical operator
  5. Special operator

Unary operations, bitwise functions, and conditional values

Unary minus changes a sign, and ! negates a logical value. For integer bits, use bitwAnd, bitwOr, or another bitwise function. Use if and else for a scalar choice or ifelse for element-wise selection.