🧮 Mathematical Functions in R: Your Magic Toolbox
Imagine you have a magic toolbox. Each tool inside does something special with numbers. Today, we’ll open this box and discover 7 amazing tools!
🎯 The Big Picture
Think of mathematical functions like kitchen appliances:
- A blender transforms fruit into smoothies
- An oven changes raw dough into bread
- A freezer converts water into ice
In R, mathematical functions transform numbers into new numbers. Let’s meet each one!
1️⃣ Absolute and Sign Functions
What Are They?
Absolute value is like measuring how far you are from home—it doesn’t matter if you went left or right, just how far.
Sign tells you which direction you went: left (-1), right (+1), or stayed home (0).
🎪 The Story
Imagine a number line on the ground. You’re standing at zero (home).
- Walk 5 steps RIGHT → You’re at +5
- Walk 5 steps LEFT → You’re at -5
Question: How far are you from home in both cases?
Answer: 5 steps! That’s the absolute value.
R Code Examples
# abs() gives distance from zero
abs(-7) # Returns 7
abs(7) # Returns 7
abs(-3.5) # Returns 3.5
# sign() tells direction
sign(-7) # Returns -1 (negative)
sign(7) # Returns 1 (positive)
sign(0) # Returns 0 (at zero)
💡 Real-World Use
- Temperature change: Whether it went up 5° or down 5°, the amount of change is 5°
- Bank balance: Gained $100 or lost $100? The magnitude is $100
2️⃣ Square Root Function
What Is It?
Square root asks: “What number, multiplied by itself, gives me this number?”
🎪 The Story
You have a square garden. Its area is 25 square meters.
Question: How long is each side?
Answer: 5 meters! Because 5 × 5 = 25.
The square root is like finding the side length from the area!
R Code Examples
# sqrt() finds the square root
sqrt(25) # Returns 5 (because 5×5=25)
sqrt(16) # Returns 4 (because 4×4=16)
sqrt(2) # Returns 1.414...
sqrt(100) # Returns 10
# You can also use ^ 0.5
25^0.5 # Returns 5 (same as sqrt)
💡 Real-World Use
- Finding the side of a square room from its area
- Calculating distances using the Pythagorean theorem
- Measuring uncertainty in statistics
3️⃣ Logarithmic Functions
What Are They?
Logarithms ask: “How many times do I multiply this base to get my number?”
🎪 The Story
You have a magic penny that doubles every day.
| Day | Amount |
|---|---|
| 0 | 1¢ |
| 1 | 2¢ |
| 2 | 4¢ |
| 3 | 8¢ |
| 10 | 1024¢ |
Question: On which day will you have 1024 pennies?
Answer: Day 10! Because 2¹⁰ = 1024.
The logarithm (base 2) of 1024 is 10.
R Code Examples
# log() is natural log (base e ≈ 2.718)
log(10) # Returns 2.302...
# log10() is log base 10
log10(100) # Returns 2 (10²=100)
log10(1000) # Returns 3 (10³=1000)
# log2() is log base 2
log2(8) # Returns 3 (2³=8)
log2(1024) # Returns 10 (2¹⁰=1024)
# Custom base: log(x, base)
log(27, 3) # Returns 3 (3³=27)
💡 Real-World Use
- Measuring earthquake strength (Richter scale)
- Sound loudness (decibels)
- pH levels in chemistry
- Data compression algorithms
4️⃣ Exponential Function
What Is It?
Exponential means “grow by multiplying”—the opposite of logarithm!
🎪 The Story
Meet the magic bacteria! One bacterium splits into two every hour.
| Hour | Bacteria |
|---|---|
| 0 | 1 |
| 1 | 2 |
| 2 | 4 |
| 3 | 8 |
This is exponential growth—things multiply fast!
The special number e (≈ 2.718) appears naturally in growth and decay.
R Code Examples
# exp() raises e to a power
exp(1) # Returns 2.718... (e¹)
exp(2) # Returns 7.389... (e²)
exp(0) # Returns 1 (e⁰=1)
# Exponential and log are opposites
exp(log(5)) # Returns 5
log(exp(5)) # Returns 5
# Any base raised to power
2^3 # Returns 8
10^2 # Returns 100
💡 Real-World Use
- Population growth
- Compound interest in banks
- Radioactive decay
- Virus spread modeling
5️⃣ Trigonometric Functions
What Are They?
Trig functions relate angles to the sides of triangles.
🎪 The Story
Imagine a ferris wheel. As you ride:
- sin() tells your height above ground
- cos() tells your distance from center
- tan() is the ratio: sin ÷ cos
It’s like describing your position on a circle!
R Code Examples
# IMPORTANT: R uses radians, not degrees!
# π radians = 180 degrees
# sin() - sine function
sin(0) # Returns 0
sin(pi/2) # Returns 1 (90°)
# cos() - cosine function
cos(0) # Returns 1
cos(pi) # Returns -1 (180°)
# tan() - tangent function
tan(0) # Returns 0
tan(pi/4) # Returns 1 (45°)
# Convert degrees to radians
degrees <- 90
radians <- degrees * pi / 180
sin(radians) # Returns 1
💡 Real-World Use
- GPS navigation
- Sound waves and music
- Building bridges
- Video game graphics
6️⃣ Rounding Functions
What Are They?
Rounding functions simplify numbers to make them cleaner.
🎪 The Story
You buy candy for $2.47. The cashier says:
- “Round to nearest dollar” → $2
- “Always round up” → $3
- “Always round down” → $2
- “Just cut off the cents” → $2
Different situations need different rounding!
R Code Examples
# round() - nearest value
round(3.7) # Returns 4
round(3.2) # Returns 3
round(3.567, 2) # Returns 3.57 (2 decimals)
# ceiling() - always round UP
ceiling(3.1) # Returns 4
ceiling(3.9) # Returns 4
ceiling(-3.1) # Returns -3
# floor() - always round DOWN
floor(3.1) # Returns 3
floor(3.9) # Returns 3
floor(-3.1) # Returns -4
# trunc() - cut toward zero
trunc(3.7) # Returns 3
trunc(-3.7) # Returns -3
💡 Real-World Use
- Money calculations (cents to dollars)
- Screen pixels (can’t have 0.5 pixels!)
- Counting items (can’t have 2.3 apples)
- Statistics reporting
7️⃣ Factorial and Combinations
What Are They?
Factorial counts how many ways to arrange things.
Combinations counts how many ways to choose things.
🎪 The Story
Factorial: You have 3 trophies to arrange on a shelf. How many ways?
- Gold, Silver, Bronze
- Gold, Bronze, Silver
- Silver, Gold, Bronze
- Silver, Bronze, Gold
- Bronze, Gold, Silver
- Bronze, Silver, Gold
6 ways! That’s 3! (3 factorial) = 3 × 2 × 1 = 6
Combinations: You have 5 friends but only 2 tickets. How many different pairs can you invite?
R Code Examples
# factorial() - arrangements
factorial(3) # Returns 6 (3×2×1)
factorial(5) # Returns 120 (5×4×3×2×1)
factorial(0) # Returns 1 (special rule!)
# choose() - combinations
choose(5, 2) # Returns 10 (pick 2 from 5)
choose(10, 3) # Returns 120 (pick 3 from 10)
choose(4, 4) # Returns 1 (only one way)
# Lottery example
choose(49, 6) # Returns 13983816
# That's 1 in 14 million chance!
💡 Real-World Use
- Probability calculations
- Lottery odds
- Seating arrangements
- Team selection
🗺️ Function Flow Summary
graph TD A["Your Number"] --> B{What do you need?} B -->|Distance from zero| C["abs, sign"] B -->|Find square side| D["sqrt"] B -->|Count multiplications| E["log, log10, log2"] B -->|Multiply rapidly| F["exp"] B -->|Work with angles| G["sin, cos, tan"] B -->|Simplify decimals| H["round, floor, ceiling"] B -->|Count arrangements| I["factorial, choose"]
🎉 Quick Reference Table
| Function | What It Does | Example |
|---|---|---|
abs(x) |
Distance from zero | abs(-5) → 5 |
sign(x) |
Direction (-1, 0, +1) | sign(-5) → -1 |
sqrt(x) |
Square root | sqrt(16) → 4 |
log(x) |
Natural logarithm | log(10) → 2.3 |
log10(x) |
Log base 10 | log10(100) → 2 |
log2(x) |
Log base 2 | log2(8) → 3 |
exp(x) |
e raised to x | exp(1) → 2.718 |
sin(x) |
Sine (radians) | sin(pi/2) → 1 |
cos(x) |
Cosine (radians) | cos(0) → 1 |
tan(x) |
Tangent (radians) | tan(pi/4) → 1 |
round(x) |
Nearest integer | round(3.7) → 4 |
floor(x) |
Round down | floor(3.9) → 3 |
ceiling(x) |
Round up | ceiling(3.1) → 4 |
trunc(x) |
Cut toward zero | trunc(-3.7) → -3 |
factorial(n) |
n × (n-1) × … × 1 | factorial(5) → 120 |
choose(n,k) |
Ways to pick k from n | choose(5,2) → 10 |
🚀 You Did It!
You’ve just learned 16 powerful functions that transform numbers in R. These are your building blocks for:
- Data analysis
- Scientific computing
- Statistical modeling
- Machine learning
Remember: Functions are just tools. The more you practice, the more natural they become. Start with simple examples, then combine them for complex calculations!
Now go play with these functions in R and watch the magic happen! ✨
