To calculate the absolute value in R, you can use the “abs()” method. The abs() function in R is “used to calculate the absolute value of a numeric data object”.
Syntax
abs(value)
Parameters
value: It is a value that needs to be converted to a positive 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 negative.
Example 1: Finding an 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.
Example 2: Calculating 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
Example 3: Calculating the 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
Example 4: Using the abs() function with 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, we defined a matrix and used it as.data.frame() method to convert R Matrix to Data Frame and then use the abs() function to absolute value.
Example 5: Calculating the Absolute Values of a data frame column in R
To calculate the absolute values of the data frame’s column, 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
Example 6: Calculate the Absolute Difference of Two Values
diff <- 19 - 21
diff_abs <- abs(diff)
diff_abs
Output
[1] 2
The diff <- 19 – 21: This line subtracts 21 from 19, which equals -2. The result is stored in the variable diff.
The diff_abs <- abs(diff): The abs() function in R returns the absolute value of a number. In this case, it’s calculating the absolute value of diff (which is -2), which equals 2. The result is stored in the variable diff_abs.
Error in Math.factor(x): ‘abs’ not meaningful for factors
This error typically occurs when you are “trying to apply a mathematical function (like abs()) to a factor variable in R”. Factor variables are categorical variables that take on a limited number of different values; they aren’t numerical and, therefore, can’t be used in most mathematical operations.
Reproduce the error
x <- factor(c("1", "2", "3"))
abs(x)
Output
Error in Math.factor(x) : ‘abs’ not meaningful for factors
To fix this error, you must “ensure that the variable you are passing to abs() (or any other mathematical function) is numerical”. If the variable is stored as a factor but represents numbers (e.g., “1”, “2”, “3”), you can convert it to numeric with the as.numeric() function.
Fixing the error
x <- factor(c("-1", "2", "-3"))
abs(as.numeric(as.character(x)))
Output
[1] 1 2 3
That’s it.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.