Math Functions in R
R provides a comprehensive set of mathematical functions to perform basic and advanced calculations. Here’s a detailed look at some common functions.
Basic Arithmetic Functions
Addition, Subtraction, Multiplication, and Division
Addition (+)
5 + 3 # Result: 8
Subtraction (–)
10 - 4 # Result: 6
Multiplication (*)
6 * 7 # Result: 42
Division (/)
15 / 3 # Result: 5
Exponentiation (^ or **)
Exponentiation
2^3 # Result: 8 4**0.5 # Result: 2 (square root of 4)
Mathematical Functions
Square Root (sqrt())
Description: Calculates the square root of a number.
Syntax:
sqrt(x)
Example:
sqrt(16) # Result: 4 sqrt(25) # Result: 5
Logarithms (log(), log10(), log2())
Description: Computes logarithms, natural base (log()), base 10 (log10()), base 2 (log2()).
Syntax:
log(x, base) log10(x) log2(x)
Examples:
log(10) # Natural logarithm of 10 log10(100) # Base 10 logarithm of 100 log2(32) # Base 2 logarithm of 32
Exponential (exp())
Description: Computes the exponential of a number.
Syntax:
exp(x)
Example:
exp(1) # Result: 2.7182818 (e) exp(2) # Result: 7.389056
Trigonometric Functions (sin(), cos(), tan())
Description: Calculates trigonometric functions for angles in radians.
Syntax:
sin(x) cos(x) tan(x)
Examples:
sin(pi/2) # Result: 1 cos(pi) # Result: -1 tan(pi/4) # Result: 1
Hyperbolic Functions (sinh(), cosh(), tanh())
Description: Computes hyperbolic functions.
Syntax:
sinh(x) cosh(x) tanh(x)
Examples:
sinh(1) # Result: 1.175201 cosh(1) # Result: 1.543081 tanh(1) # Result: 0.761594
Absolute Value (abs())
Description: Computes the absolute value of a number.
Syntax:
abs(x)
Example:
abs(-5) # Result: 5 abs(3) # Result: 3
Ceiling (ceiling()) and Floor (floor())
Description: Rounds up to the nearest integer (ceiling()) or down to the nearest integer (floor()).
Syntax:
ceiling(x) floor(x)
Examples:
ceiling(2.3) # Result: 3 floor(2.7) # Result: 2
Rounding (round(), trunc(), signif())
Description: Rounds numbers to a specified number of significant digits.
Syntax:
round(x, digits) trunc(x) signif(x, digits)
Examples:
round(3.14159, 2) # Result: 3.14 trunc(3.14159) # Result: 3 signif(3.14159, 3) # Result: 3.14
Complex Numbers (Re(), Im(), Mod(), Arg())
Description: Manipulates complex numbers.
Syntax:
Re(z) Im(z) Mod(z) Arg(z)
Example:
z <- 3 + 4i Re(z) # Result: 3 Im(z) # Result: 4 Mod(z) # Result: 5 Arg(z) # Result: 0.9272952 (radians)
Practical Examples and Exercises
Solve a Quadratic Equation
Example: Solve the quadratic equation ax2+bx+c=0ax^2 + bx + c = 0ax2+bx+c=0.
a <- 1 b <- -3 c <- 2 # Calculate roots discriminant <- b^2 - 4*a*c root1 <- (-b + sqrt(discriminant)) / (2*a) root2 <- (-b - sqrt(discriminant)) / (2*a) root1 # Result: 2 root2 # Result: 1
Calculate the Area of a Circle
Example: Use the pi function to calculate the area of a circle with radius 5.
radius <- 5 area <- pi * radius^2 area # Result: 78.53982
Convert Between Degrees and Radians
Example: Convert 90 degrees to radians.
degrees <- 90 radians <- degrees * pi / 180 radians # Result: 1.570796
Calculate the Compound Interest
Example: Calculate the compound interest for a principal of $1000 with an annual interest rate of 5% over 3 years.
principal <- 1000 rate <- 0.05 time <- 3 compound_interest <- principal * (1 + rate)^time - principal compound_interest # Result: 157.625