R abs()
R abs() function is used to compute the absolute value of the given argument.
In this tutorial, we will learn how to use abs()
function to compute the absolute of given value.
Syntax
The syntax of abs()
function to compute absolute value of x
is
</>
Copy
abs(x)
Returns the absolute value of x
.
x
can be a numeric or complex vector or array.
Examples
In the following program, we will find the absolute value of a number -16
.
example.R
</>
Copy
x <- -16
result <- abs(x)
cat("The absolute value of", x, "is :", result)
Output
The absolute value of -16 is : 16
Now, let us take an array of numbers, and find its absolute value.
example.R
</>
Copy
x <- array(c(1, -16, -9), dim=c(3,1))
result <- abs(x)
cat("The absolute value of array [", x, "] is :", result)
Output
The absolute value of array [ 1 -16 -9 ] is : 1 16 9
Conclusion
In this R Tutorial, we learned how to find the absolute value of a numeric value using abs()
function, with the help of examples.