🔢 R Numbers & Math – Perform Arithmetic and Numeric Operations in R
🧲 Introduction – Numbers and Math in R Programming
R is fundamentally a numeric computing language, built to handle everything from simple arithmetic to complex statistical modeling. Understanding how R treats numbers and performs mathematical operations is essential for any data analysis or machine learning task.
🎯 In this guide, you’ll learn:
- How numbers are represented in R
- Arithmetic, relational, and mathematical functions
- How to work with integers, decimals, and special values
- Practical math operations including rounding, exponentials, and trigonometry
🔤 Types of Numbers in R
| Type | Example | Description | 
|---|---|---|
| numeric | 3.14 | Default type for real numbers | 
| integer | 42L | Whole numbers, declared with L | 
| complex | 2+3i | Real + imaginary numbers | 
| double | 5.5 | Subtype of numeric (64-bit float) | 
a <- 10      # numeric
b <- 5L      # integer
c <- 2 + 3i  # complex
Use class() and typeof() to inspect:
class(a)     # "numeric"
typeof(b)    # "integer"
is.complex(c) # TRUE
➕ Basic Arithmetic Operators
| Operator | Operation | Example | 
|---|---|---|
| + | Addition | x + y | 
| - | Subtraction | x - y | 
| * | Multiplication | x * y | 
| / | Division | x / y | 
| %% | Modulo (remainder) | x %% y | 
| %/% | Integer division | x %/% y | 
| ^or** | Exponentiation | x ^ y | 
x <- 15
y <- 4
x + y     # 19
x %% y    # 3
x %/% y   # 3
x ^ 2     # 225
📐 Built-in Math Functions
R provides a rich set of built-in math functions.
| Function | Description | Example | 
|---|---|---|
| abs(x) | Absolute value | abs(-5)=5 | 
| sqrt(x) | Square root | sqrt(16)=4 | 
| log(x) | Natural log (base e) | log(10) | 
| log10(x) | Base-10 log | log10(100) | 
| exp(x) | Exponential | exp(2) | 
| round(x) | Round to nearest integer | round(3.65) | 
| ceiling(x) | Round up | ceiling(4.1) | 
| floor(x) | Round down | floor(4.9) | 
| sign(x) | Sign of number (-1, 0, 1) | sign(-10) | 
✅ Example:
x <- -3.5
abs(x)        # 3.5
round(x)      # -4
ceiling(x)    # -3
floor(x)      # -4
📏 Trigonometric Functions
Useful in signal processing and scientific applications:
| Function | Description | 
|---|---|
| sin(x) | Sine (x in radians) | 
| cos(x) | Cosine | 
| tan(x) | Tangent | 
| asin(x) | Arc sine | 
| acos(x) | Arc cosine | 
| atan(x) | Arc tangent | 
theta <- pi / 4
sin(theta)    # ≈ 0.707
cos(theta)    # ≈ 0.707
🧪 Constants and Special Values
R includes some predefined numeric constants:
| Constant | Value | 
|---|---|
| pi | 3.141593… | 
| Inf | Infinity | 
| -Inf | -Infinity | 
| NaN | Not a Number | 
| NA | Missing value | 
log(0)        # -Inf
1 / 0         # Inf
sqrt(-1)      # NaN
Use:
is.infinite(Inf)   # TRUE
is.nan(NaN)        # TRUE
is.na(NA)          # TRUE
📌 Summary – Recap & Next Steps
Numbers and math form the core of statistical programming in R. From simple arithmetic to advanced math functions, R provides everything needed for robust numeric computation.
🔍 Key Takeaways:
- Use numeric,integer, andcomplextypes to handle data precisely
- Use operators like +,%%,^, and%/%for arithmetic operations
- Leverage built-in functions for rounding, logs, exponentials, and trigonometry
- Handle special values like NA,NaN, andInfappropriately in your code
⚙️ Real-World Relevance:
Mastering numeric operations in R enables tasks like financial modeling, signal processing, machine learning, and statistical simulations—all grounded in efficient numeric computation.
❓ FAQs – Numbers and Math in R
❓ How do I ensure a value is stored as an integer?
✅ Add L to the number:
x <- 42L
is.integer(x)  # TRUE
❓ What does %% do in R?
✅ It’s the modulo operator. It returns the remainder:
10 %% 3  # Output: 1
❓ How can I perform integer division in R?
✅ Use %/% to return the integer quotient:
10 %/% 3  # Output: 3
❓ How does R handle NaN, NA, and Inf?
✅ R propagates these values through calculations. Use is.nan(), is.na(), and is.infinite() to detect them.
❓ Can I use degrees in trigonometric functions?
✅ No. R uses radians. Convert degrees using:
deg2rad <- function(deg) deg * pi / 180
sin(deg2rad(30))  # ≈ 0.5
Share Now :
