Absolute Value in R (Example)
This article shows how to apply the abs() function in order to compute the absolute value of a numeric value in the R programming language.
Example Data
We use a simple numeric vector as example data. Note that some of the values of our vector are negative:
vec <- c(5, -3, 2, -5, -1) # Create example vector vec # Print example vector # 5 -3 2 -5 -1 |
vec <- c(5, -3, 2, -5, -1) # Create example vector vec # Print example vector # 5 -3 2 -5 -1
Calculate Absolute Value in R (abs Function)
If we want to convert our numeric vector to strictly positive, we can apply the abs function as follows:
abs(vec) # Apply abs function # 5 3 2 5 1 |
abs(vec) # Apply abs function # 5 3 2 5 1
As you can see, all negative values were replaced by the corresponding positive value.
Video: abs Function in R

By loading the video, you agree to YouTube’s privacy policy.
Learn more
2 Comments. Leave new
awesome work . need numbering of each lesson
Thanks for the kind comment and suggestion, Abdul!