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

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

As you can see, all negative values were replaced by the corresponding positive value.

Video: abs Function in R

YouTube

By loading the video, you agree to YouTube’s privacy policy.
Learn more

Load video

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top