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))
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources