R abs Function

The R abs method is one of the R Math functions, which is to return the Positive absolute value of a specific number or an expression. Let us see how to use abs in R Programming language with an example.

The syntax of the abs in R Programming language is as shown below:

abs(number); //Return Type is Integer

Number: It can be a number or a valid numerical expression for which you want to find absolute value.

  • If the number argument is positive or negative zero, the abs function returns positive zero.
  • If the number argument is not a number, abs function returns Error.

R abs example

The abs function allows you to find the absolute values of a numeric value. In this program, We are going to find the absolute values of different data and display the output

# R abs Function example

print("The abosolute Values are:")
# Absolute Values of both Positive and negative zeros

# Absolute values of Positive values

# Absolute Values of Negative values

# abs on vectors
num <- c(-1.526, 2, -3.5, -4 , -52.7896)
R abs Function 1

R abs Function Example 2

The ABS Function in R also allows you to find the absolute values of a column value. In this example, we are going to find the absolute values for all the records present in [Service Grade] column using the abs Function.

For this R absolute positive demonstration, We are going to use the below-shown CSV data. I suggest you refer to the R Read CSV Function article to understand the steps involved in importing the CSV file in R Programming.

R abs Function 3


# abs Function example


product <- read.csv("Product_Data.csv", TRUE, sep = ",")

# Absolute Value

From the below screenshot, you can observe that abs function is returning the absolute values of the Service grade column

R abs Function 2

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.