R Math
R provides a wide range of mathematical functions for performing calculations and data analysis. These functions include square root, exponentiation, logarithmic operations, trigonometric functions, rounding, and more.
Key Topics
1. Comprehensive Math Functions
R includes built-in functions for a variety of mathematical operations such as square root, exponentiation, absolute values, and logarithms.
# Comprehensive math functions
sqrt_value <- sqrt(25) # Square root
exp_value <- exp(1) # Exponential (e^1)
log_value <- log(100) # Natural logarithm (base e)
log10_value <- log10(100) # Logarithm base 10
log2_value <- log2(8) # Logarithm base 2
abs_value <- abs(-42) # Absolute value
ceiling_value <- ceiling(4.2) # Ceiling value
floor_value <- floor(4.7) # Floor value
print(sqrt_value)
print(exp_value)
print(log_value)
print(log10_value)
print(log2_value)
print(abs_value)
print(ceiling_value)
print(floor_value)
Output:
[1] 2.718282
[1] 4.60517
[1] 2
[1] 3
[1] 42
[1] 5
[1] 4
Code Explanation: The code demonstrates the use of various math functions in R: sqrt()
for square root, exp()
for the exponential function, log()
for the natural logarithm, log10()
for logarithm base 10, log2()
for logarithm base 2, abs()
for the absolute value, ceiling()
for rounding up, and floor()
for rounding down.
2. Trigonometric Functions
R provides trigonometric functions like sine, cosine, and tangent, which use radians as input.
# Trigonometric functions
sin_value <- sin(pi / 6) # Sine of 30 degrees (pi/6 radians)
cos_value <- cos(pi / 3) # Cosine of 60 degrees (pi/3 radians)
tan_value <- tan(pi / 4) # Tangent of 45 degrees (pi/4 radians)
print(sin_value)
print(cos_value)
print(tan_value)
Output:
[1] 0.5
[1] 1
Code Explanation: The trigonometric functions sin()
, cos()
, and tan()
calculate the sine, cosine, and tangent of the input angles (in radians).
3. Rounding Functions
R offers functions to round numbers to the nearest integer or a specified number of decimal places.
# Rounding functions
round_value <- round(3.14159, 2) # Round to 2 decimal places
ceiling_value <- ceiling(6.7) # Ceiling (round up)
floor_value <- floor(6.7) # Floor (round down)
trunc_value <- trunc(6.7) # Truncate to integer
print(round_value)
print(ceiling_value)
print(floor_value)
print(trunc_value)
Output:
[1] 7
[1] 6
[1] 6
Code Explanation: The rounding functions in R include round()
for rounding to a specified number of decimal places, ceiling()
for rounding up, floor()
for rounding down, and trunc()
for truncating the decimal part.
4. Math Function Table
Below is a table listing additional math functions in R and their explanations:
Function | Description |
---|---|
sqrt(x) | Calculates the square root of x . |
exp(x) | Computes the exponential of x (e^x). |
log(x, base) | Computes the logarithm of x with the specified base (default is natural logarithm). |
log10(x) | Computes the base-10 logarithm of x . |
log2(x) | Computes the base-2 logarithm of x . |
abs(x) | Returns the absolute value of x . |
ceiling(x) | Rounds x up to the nearest integer. |
floor(x) | Rounds x down to the nearest integer. |
trunc(x) | Truncates x to the integer part. |
round(x, digits) | Rounds x to the specified number of decimal places. |
sign(x) | Returns the sign of x (-1 for negative, 0 for zero, 1 for positive). |
cos(x) | Computes the cosine of x (in radians). |
sin(x) | Computes the sine of x (in radians). |
tan(x) | Computes the tangent of x (in radians). |
acos(x) | Computes the arc cosine of x (in radians). |
asin(x) | Computes the arc sine of x (in radians). |
atan(x) | Computes the arc tangent of x (in radians). |
cosh(x) | Computes the hyperbolic cosine of x . |
sinh(x) | Computes the hyperbolic sine of x . |
tanh(x) | Computes the hyperbolic tangent of x . |
gamma(x) | Computes the gamma function of x . |
lgamma(x) | Computes the natural logarithm of the gamma function of x . |
factorial(x) | Computes the factorial of x . |
Key Takeaways
- R provides a comprehensive set of mathematical functions for various operations.
- Trigonometric functions use radians, not degrees, as input.
- Rounding functions are essential for handling numerical precision in data analysis.