Absolute Value in R

Sometimes in math, it is necessary to compare the magnitude of numbers regardless of whether or not they are positive, negative imaginary, or complex. Unfortunately, direct comparisons do not produce meaningful results. For example, -20 is less than 5 but the magnitude of -20 is greater than the magnitude of 5. This magnitude is called the absolute value and it is very useful in comparing all four types of numbers in mathematical settings.

Description of the function.

Finding the absolute value in r involves using the absolute value function or abs function which takes the form of abs(x) where x is any real number or numerical value. This absolute value equation always produces a positive number even when x is negative or a complex number. This function can take as an argument a single variable, a vector, a matrix, or a data frame, however, the data frame pattern needs to be entirely numeric or you will get an error message problem. The absolute value function returns the original number output if that number is a positive value, but it returns the negative of the original number if that number is a negative value. This means that it produces a positive number interval in both cases.

Examples of the function in action.

The absolute value function is extremely easy to use because it only takes one argument, even for a whole dataframe.

# absolute value in r
> abs(5)
[1] 5
> abs(-5)
[1] 5

In this extremely simple example, we enter the five and a negative five. Note that in both cases the result is a positive number pattern.

# how to calculate absolute value in r
> a = c(-3:3)
> a
[1] -3 -2 -1 0 1 2 3
> abs(a)
[1] 3 2 1 0 1 2 3

In this more complicated interval example, we are using a vector with values ranging from negative three to positive three. Note that despite the fact that the first three numbers in the numerical value vector are negative the results are all positive in all mathematical settings.

Application of this function.

The absolute value equation graphically represents the line of a vector on a number line from zero to the point the vector represents. By providing this absolute difference or magnitude the function’s output is able to provide the real absolute difference in scale between different types of numbers, including a negative number or coefficient element with the absolute value expression. This function makes it possible to compare the scale of both positive and negative numbers. A real-world application of this that can show up in data science would be comparing the distance traveled by two or more vehicles going in different directions. In such a case the direction does not matter only the total distance traveled.

A more general alternative function

Unfortunately, the absolute value equation does not produce results for an imaginary or complex number. The following object example creates a user-defined function that adds the syntax element needed to handle imaginary and complex numbers in the real valued function or absolute value expression.

# absolute value function in r
> a= c(4,5)
> b = c(4,-5)
> c = 8
> d = -8
> abscn = function(x) {
+ if (length(x) == 1) z = abs(x) else z = sqrt(x[1]^2 + x[2]^2)
+ return(z)
+ }
> abscn(a)
[1] 6.403124
> abscn(b)
[1] 6.403124
> abscn(c)
[1] 8
> abscn(d)
[1] 8

In this user-defined function notation, the basic idea is to represent a complex or imaginary number in a vector space in parenthesis with the real position being first in the imaginary being second. It also accepts a variable of length one as a real number. This is a real number line example that shows how you can use additional R code syntax to overcome the shortcomings of built-in function notation.

Finding an absolute value in r is a simple process but it is limited to real numbers. However, it only takes a little bit of simple code to overcome that shortcoming.

Looking for more cool R programming content? Check out these other great articles: