R Core Language Concepts
Estimated reading: 3 minutes 25 views

🔢 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

TypeExampleDescription
numeric3.14Default type for real numbers
integer42LWhole numbers, declared with L
complex2+3iReal + imaginary numbers
double5.5Subtype 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

OperatorOperationExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%%Modulo (remainder)x %% y
%/%Integer divisionx %/% y
^ or **Exponentiationx ^ 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.

FunctionDescriptionExample
abs(x)Absolute valueabs(-5) = 5
sqrt(x)Square rootsqrt(16) = 4
log(x)Natural log (base e)log(10)
log10(x)Base-10 loglog10(100)
exp(x)Exponentialexp(2)
round(x)Round to nearest integerround(3.65)
ceiling(x)Round upceiling(4.1)
floor(x)Round downfloor(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:

FunctionDescription
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:

ConstantValue
pi3.141593…
InfInfinity
-Inf-Infinity
NaNNot a Number
NAMissing 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, and complex types 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, and Inf appropriately 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

R – Numbers & Math

Or Copy Link

CONTENTS
Scroll to Top