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 abs(0) abs(-0) # Absolute values of Positive values abs(20) abs(25.659) # Absolute Values of Negative values abs(-10.0897) abs(-23.659) # abs on vectors num <- c(-1.526, 2, -3.5, -4 , -52.7896) abs(num)
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.
# abs Function example getwd() product <- read.csv("Product_Data.csv", TRUE, sep = ",") print(product) # Absolute Value abs(product$Service.Grade)
From the below screenshot, you can observe that abs function is returning the absolute values of the Service grade column