The R abs method is one of the R Math function, which is used to return the absolute Positive value of an individual number, or an expression. In this article, we will show you, How to use abs in R Programming language with an example.
R abs syntax
The basic 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, abs function will return positive zero.
- If the number argument is not a number, abs function will return 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 abs Function.
For this 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 CSV file.
# R abs Function example getwd() product <- read.csv("Product_Data.csv", TRUE, sep = ",") print(product) # Absolute Value abs(product$Service.Grade)
From the above screenshot, you can observe that abs function is returning the absolute values of the Service grade column
Thank You for Visiting Our Blog