How to Calculate Absolute Value in R

You often come across a scenario in which you have to compare the magnitude of numbers regardless of whether or not they are positive, negative, imaginary, or complex. Unluckily, immediate comparisons do not produce meaningful results. For example, -21 is less than 19, but the magnitude of -21 is greater than the magnitude of 19. That magnitude is called an absolute value. 

R abs() is a built-in Math function that is used to calculate the absolute values. In addition, the abs() function returns the positive value of specific numeric data or an expression. 

Absolute value in R

To calculate the absolute value in R, use the abs() method. The abs() function takes a real number or numeric value as a vector, matrix, or data frame and returns the absolute value.

The abs() is a built-in method used to get the absolute value in which it converts a negative value into a positive value, and if there is a positive value, then it doesn’t change.

The abs() function always returns a positive number even when the input value is negative or a complex number. 

Syntax

abs(value)

Parameters

The abs() function returns the absolute value.

Return Value

The abs() function returns the original number output if that number is a positive value. Still, it returns the negative of the original number if that number is a negative value.

Example to find absolute value in R

answer1 <- abs(-21)
answer2 <- abs(19)
answer3 <- abs(-46)

answer1
answer2
answer3

Output

[1] 21
[1] 19
[1] 46

You can see that the abs() function turns a negative value into a positive value.

Calculate the absolute value of the vector in R

To calculate the absolute value of the vector in R, use the abs() function.

data1 <- abs(c(11, -21, 19))
data2 <- abs(c(-18, 29, -46))

data1
data2

Output

[1] 11 21 19
[1] 18 29 46

Compute absolute value of a Matrix in R

To calculate the absolute value of the matrix, use the R abs() method.

mtrx <- matrix(c(-21, 19, -51, 29, -46, 18),
 nrow = 3,
 ncol = 2,
 byrow = TRUE)

mtrx
cat("Absolute value of matrix", "\n")
abs_mtrx <- abs(mtrx)
abs_mtrx

Output

      [,1] [,2]
[1,]  -21   19
[2,]  -51   29
[3,]  -46   18
Absolute value of matrix
      [,1] [,2]
[1,]   21   19
[2,]   51   29
[3,]   46   18

Apply abs() function to data.frame

We can use the abs() function to find the absolute value of the column in the data frame.

We will create a data frame using the above matrix and as.data.frame() function.

mtrx <- matrix(c(-21, 19, -51, 29, -46, 18),
 nrow = 3,
 ncol = 2,
 byrow = TRUE)

cat("The matrix is: ", "\n")
mtrx
df <- as.data.frame(mtrx)
cat("The data frame is: ", "\n")
df
abs_df <- abs(df)
cat("The absolute value of data frame is: ", "\n")
abs_df

Output

The matrix is:
     [,1] [,2]
[1,] -21   19
[2,] -51   29
[3,] -46   18
The data frame is:
      V1   V2
1    -21   19
2    -51   29
3    -46   18
The absolute value of data frame is:
      V1   V2
1     21   19
2     51   29
3     46   18

In this example, first, we have defined a matrix and then use the as.data.frame() method to convert R Matrix to Data Frame and then use the abs() function to absolute value.

Calculate Absolute Values of a data frame column in R

To calculate the absolute values of the column of the data frame, use the abs() function. We will use the above data frame to demonstrate the example.

mtrx <- matrix(c(-21, 19, -51, 29, -46, 18),
 nrow = 3,
 ncol = 2,
 byrow = TRUE)

cat("The matrix is: ", "\n")
mtrx
df <- as.data.frame(mtrx)
cat("The data frame is: ", "\n")
df
df_col_abs <- df
df_col_abs$V1 <- abs(df_col_abs$V1)
df_col_abs

Output

[,1] [,2]
[1,] -21 19
[2,] -51 29
[3,] -46 18
The data frame is:
   V1   V2
1 -21   19
2 -51   29
3 -46   18
   V1   V2
1  21   19
2  51   29
3  46   18

Application of abs() function

The abs() function in R makes it possible to compare the scale of both positive and negative numbers. For example, ars. A real-world application would be comparing the distance traveled by two or more vehicles going in different directions.

The absolute value graphically represents the line of a vector on a number line from zero to the point the vector represents. Thus, bys. By providing the absolute difference or magnitude of the function’s output, it can provide a real absolute difference in scale between different types of numbers.

That is it for the R absolute value tutorial.

See also

R round()

Square root in R

NaN in R

max() in R

min() in R

Leave a Comment