Chapter 13: R Math
R Math (mathematical functions and operations in R).
R was literally designed for mathematics and statistics, so its math support is excellent — fast, vectorized (works on whole vectors at once), and packed with built-in functions. You rarely need external packages for basic/advanced math.
I’m going to explain it like your friendly offline teacher: slow, detailed, with tons of copy-paste examples you can run right now in RStudio, explanations of why things work this way, common traps, and 2026 modern usage tips.
1. First: Basic Arithmetic Operators (The Calculator Part)
These are the most used every day:
| Operation | Symbol | Example code | Result | Notes |
|---|---|---|---|---|
| Addition | + | 5 + 3 | 8 | — |
| Subtraction | – | 10 – 4.2 | 5.8 | — |
| Multiplication | * | 6 * 7 | 42 | — |
| Division | / | 100 / 4 | 25 | — |
| Exponentiation | ^ or ** | 2 ^ 10 or 3 ** 4 | 1024 or 81 | Both work, ^ is more common |
| Modulo (remainder) | %% | 17 %% 5 | 2 | Very useful in loops/indexing |
| Integer division | %/% | 17 %/% 5 | 3 | Floor division |
Vectorized magic (this is why R feels powerful):
|
0 1 2 3 4 5 6 7 8 9 |
temps_hyd <- c(28.5, 29.8, 30.2, 27.9, 31.1) temps_fahrenheit <- temps_hyd * 9/5 + 32 # whole vector at once! print(temps_fahrenheit) # [1] 83.3 85.64 86.36 82.22 88.0 |
2. Most Important Math Functions (Base R – No Package Needed)
Run ?Math in R console to see the full list — here are the ones you use 90% of the time:
| Function | What it does | Example | Result / Notes |
|---|---|---|---|
| abs() | Absolute value | abs(-7.2) | 7.2 |
| sqrt() | Square root | sqrt(144) | 12 |
| exp() | Exponential function (e^x) | exp(1) | ≈2.718 |
| log() | Natural log (ln) | log(100) | ≈4.605 |
| log10() | Log base 10 | log10(1000) | 3 |
| log2() | Log base 2 | log2(8) | 3 |
| round() | Round to n decimals | round(3.14159, 2) | 3.14 |
| floor() | Largest integer ≤ x | floor(3.9) | 3 |
| ceiling() | Smallest integer ≥ x | ceiling(3.1) | 4 |
| trunc() | Truncate toward zero | trunc(-3.9) | -3 |
| sign() | Sign: -1 / 0 / 1 | sign(c(-5, 0, 7)) | -1 0 1 |
| factorial() | n! | factorial(5) | 120 |
| choose() | Binomial coefficient nCr | choose(5, 2) | 10 |
Trigonometric & hyperbolic (in radians!):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
sin(pi/2) # 1 cos(0) # 1 tan(pi/4) # 1 asin(0.5) # pi/6 ≈ 0.5236 acos(0) # pi/2 ≈ 1.5708 sinh(1) # hyperbolic sine cosh(0) # 1 tanh(2) |
3. Special Math Constants Built-in
|
0 1 2 3 4 5 6 7 |
pi # 3.14159265358979 exp(1) # e ≈ 2.71828182845905 |
No built-in golden ratio, but easy:
|
0 1 2 3 4 5 6 |
(1 + sqrt(5)) / 2 # ≈ 1.618034 |
4. Vectorized = Super Fast & Clean
This is R’s superpower:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# One value sqrt(16) # 4 # Whole vector at once numbers <- c(4, 9, 16, 25, 36) sqrt(numbers) # 2 3 4 5 6 # Multiple functions log10(numbers) # 0.60206 0.95424 1.20412 1.39794 1.55630 round(sqrt(numbers), 1) # 2.0 3.0 4.0 5.0 6.0 |
5. Common Gotchas & Floating-Point Reality
Floating-point arithmetic is not exact:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
0.1 + 0.2 == 0.3 # FALSE !!!! 0.1 + 0.2 # 0.30000000000000004 # Safe comparison (from dplyr or base) all.equal(0.1 + 0.2, 0.3) # TRUE # or abs(0.1 + 0.2 - 0.3) < 1e-10 # TRUE |
Division by zero:
|
0 1 2 3 4 5 6 7 8 |
1 / 0 # Inf -1 / 0 # -Inf 0 / 0 # NaN |
6. Your Mini Practice Session (Copy → Run Now!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# Hyderabad temperature conversion example celsius <- c(28.5, 29.8, 30.2, 27.9, 31.1, 32.4) fahrenheit <- celsius * 9/5 + 32 # Math on vectors hot_threshold_f <- 86 is_hot <- fahrenheit >= hot_threshold_f cat("Temperatures (°F):", round(fahrenheit, 1), "\n") cat("How many hot days? ", sum(is_hot), "\n") cat("Average °C:", round(mean(celsius), 1), "| Geometric mean:", round(exp(mean(log(celsius))), 1), "\n") # Some fun math angles_deg <- seq(0, 360, by = 45) angles_rad <- angles_deg * pi / 180 sines <- sin(angles_rad) cosines <- cos(angles_rad) data.frame( Degrees = angles_deg, Radians = round(angles_rad, 3), Sin = round(sines, 3), Cos = round(cosines, 3) ) |
7. Quick Summary Cheat-Sheet
- Arithmetic: + – * / ^ %% %/%
- Roots & logs: sqrt(), log(), log10(), log2(), exp()
- Rounding: round(), floor(), ceiling(), trunc()
- Trig: sin(), cos(), tan(), asin(), etc. (radians!)
- Constants: pi, exp(1)
- Vectorized → apply to whole lists/vectors automatically
- Compare floats → all.equal() or small epsilon, never ==
Want to go deeper?
- Special functions (gamma(), beta(), bessel*()) ?
- Matrix algebra (%*%, solve(), eigen()) ?
- Or practice building a small math tool (BMI calculator, compound interest)?
Just tell me — next whiteboard page is ready! ☕📐🚀
