How to calculate abs() of a value in R

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

Figure 1 shows a mathematical representation of the abs() function.

Figure 1: Mathematical representation of abs() function

Syntax

abs(num)
## where num is the number whose absolute value is required

Parameter

The abs() function takes a number as a parameter.

Return value

The abs() function returns the absolute value of the number passed as a parameter.

Code

#Positive number
a <- abs(5);
print(paste0("abs(5): ", a))
#negative number
b <- abs(-5);
print(paste0("abs(-5): ", b))
#zero
c <- abs(0);
print(paste0("abs(0): ", c))