1. Arithmetics - JulTob/R GitHub Wiki

⚔️ Chart of Arithmetics

Welcome, bold one! On this page, we master R’s arithmetic cutlasses and truth-seeking compasses. Here be yer cannonballs:

➕ Basic Arithmetic

These operations work on numbers, vectors, and even whole dataframes:

> 2 + 2
[1] 4       # Addition

> 2 * 3
[1] 6       # Multiplication

> 5 - 7
[1] -2      # Subtraction

> 9 / 3
[1] 3       # Division

Each result begins with [1], remindin’ ye it be the first value in a vector.

🔺 Powers and Roots

> 3^2
[1] 9       # Exponentiation

> sqrt(4)
[1] 2       # Square root

⚠️ ² (the superscript symbol) will not work in R! Ye must use ^ for raising to a power.

> 2^3       # 2 cubed
[1] 8

🌬️ Exponentials

> exp(1)
[1] 2.718282  # e^1, where e ≈ 2.718

Useful when working with logarithms or models with exponential growth.

🧭 Logical Comparisons

These return TRUE or FALSE, the compass points of control flow.

> 1 == 1
[1] TRUE     # Equal to

> 1 != 1
[1] FALSE    # Not equal to

> 3 < 5
[1] TRUE     # Less than

> 7 >= 7
[1] TRUE     # Greater than or equal

Note the double equals == for comparison. A single = is for assignment (though <- be the captain’s preferred weapon).

🏴 Truths to Know

  • R’s logical outputs are proper Boolean values: TRUE, FALSE
  • They can even be used in math:
> TRUE + TRUE
[1] 2

> FALSE * 10
[1] 0

These tricks come in handy in filters and counters.