R Math

R Math Functions

R is fundamentally designed for mathematics and highly advanced statistical analysis.

It provides a vast library of built-in math functions that are incredibly easy to use.


Basic Arithmetic

As we saw previously, you can simply type mathematical expressions using operators like +, -, *, and /.

However, for more complex equations, you rely heavily on built-in math functions.


Common Math Functions

The max() and min() functions rapidly find the highest or lowest value in a provided list of numbers.

The sqrt() function precisely calculates the square root of a specific number.

The abs() function instantly returns the absolute (positive) value of a number.

Math Functions Example:

# Finding the maximum value
highest_value <- max(5, 10, 15)
print(highest_value) # 15
# Finding the square root
square_root <- sqrt(16)
print(square_root) # 4
# Absolute positive value
absolute_val <- abs(-4.7)
print(absolute_val) # 4.7

Rounding Numbers

When dealing with decimals, you frequently need to round them to whole integers.

The ceiling() function explicitly rounds a number upwards to its nearest whole integer.

The floor() function explicitly rounds a number downwards to its nearest whole integer.


Exercise 1 of 2

?

Which function returns the highest number in a specified list?

Exercise 2 of 2

?

Which function forces a decimal number to strictly round UPWARDS to the nearest whole integer?