In this tutorial, we will look at how to get the absolute value for a variable (or values in a vector) in R with the help of some examples.
How to get the absolute value of a number in R?
You can use the built-in math function abs()
to get the absolute value (magnitude without the sign) of a number in R. Pass the number for which you want to get the absolute value as an argument to the abs()
function. The following is the syntax –
abs(x)
It returns the number without any sign. That is, if you pass a negative number, it will return the number without the negative sign and if you pass a positive number, it will return the same number.
Note that you can apply the abs()
function to a vector, array, matrix, and a dataframe as well.
Examples
Let’s look at some examples of using the abs()
function in R.
Apply the abs()
function to a number
Let’s apply the abs()
function directly to a positive and a negative number of the same magnitude, for example, 5 and -5, and compare the output.
# absolute value of a positive number print(abs(5)) # absolute value of a negative number print(abs(-5))
Output:
[1] 5 [1] 5
We get the absolute value (5, in this example) without any sign in both cases.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Apply the abs()
function to a numeric vector
You can similarly apply the abs()
function to a vector in R. Note that individual values in R can be regarded as a vector of length one. If you apply the abs()
function to a numeric vector, it will compute the absolute value for each value in the vector.
Let’s look at an example.
# absolute value of a vector vec <- c(1, -2, 3, -4, -5) print(abs(vec))
Output:
[1] 1 2 3 4 5
You can see that we get a vector with absolute values for each value in the passed vector. The positive values are unchanged whereas for the negative values we get the corresponding absolute value (without the sign).
You might also be interested in –
- Calculate Median of a Vector in R
- Get the Maximum value in an R Vector
- Get the Minimum Value in an R Vector
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.