Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
In R, the absolute value of a number is returned by the base function abs(): abs(-7) gives 7. The same call works on whole vectors, matrices and numeric data-frame columns, and for complex numbers it returns the modulus. This guide covers the exact behaviour documented in the R manual, the data-frame rule that trips people up, and the research tasks where absolute values appear (absolute error, absolute residuals, two-sided p-values).
What absolute value in R is used for in research
Absolute value removes the sign and keeps the size of a number. In analysis scripts this shows up in four places:
- Absolute error.
abs(observed - predicted)measures how far each prediction is from the data regardless of direction; its mean is the mean absolute error. - Absolute residuals. Taking
abs()of model residuals is a common step when checking whether spread changes with the fitted value (see how to read a residual plot for homoscedasticity). - Two-sided tests. A two-sided p-value from a z statistic is written
2 * pnorm(-abs(z)), whereabs()makes the result independent of the sign of z (worked through in pnorm in R; background on the p-value). - Distances and differences. The gap between two measurements, such as a pre/post change, is
abs(a - b)when direction does not matter.
Install or access (official source only)
abs() is part of base R, so no package needs to be installed or loaded. R itself is distributed from the Comprehensive R Archive Network at cran.r-project.org. The function is documented in the base manual page “Miscellaneous Mathematical Functions” (?abs in an R session opens the same page).
Step-by-step: take the absolute value in R
- Single number. Run
abs(-12). The result is12. - Vector. Run
x <- c(-3, 2, 0, -1.5)and thenabs(x). Each element is converted separately and the result is3 2 0 1.5. - Matrix. Run
m <- matrix(c(-1, 2, -3, 4), nrow = 2)and thenabs(m). The manual lists “a numeric or complex vector or array” as the argument, so the matrix keeps its shape. - One data-frame column. Run
df$change <- abs(df$change)to replace a numeric column with its absolute values, or assign to a new column name to keep the original. - Every numeric column. With dplyr, run
library(dplyr)and thendf |> mutate(across(where(is.numeric), abs)). In base R the equivalent isnum <- sapply(df, is.numeric)followed bydf[num] <- lapply(df[num], abs). - Absolute difference of two values. Run
abs(a - b).
What the R manual says about abs()
These points come from the base R manual page “Miscellaneous Mathematical Functions” (R documentation, checked 2026-10-29):
- Return type. “abs(x) returns an integer vector when x is integer or logical.” So
abs(-3L)is an integer, whileabs(-3)is a double. Check withclass()ortypeof()if a later step depends on integer storage. - Complex numbers. For complex arguments,
abs(z) == Mod(z). The modulus ofz = x + yiis the square root of x² + y² (complex-number manual page), soabs(3+4i)returns5. - Generic function.
absis an internal generic primitive: methods can be defined for individual classes or through theMathgroup generic.
Output and how to read it
abs() always returns an object with the same length as its input and no negative entries for real input. A vector in gives a vector out; a matrix in gives a matrix out. For research use, the output is rarely the end point: it normally feeds a summary such as mean(abs(obs - pred)) for mean absolute error or median(abs(res)) for the median absolute residual. Note that the mean absolute deviation around a centre (mean(abs(x - mean(x)))) is a different quantity from the function mad(), which is a median-based measure; for the summary functions R provides, see summary statistics in R. If the result needs rounding for a table, see rounding in R.
Troubleshooting
abs() fails on a data frame
The R documentation for the group generics states that the data-frame methods for the “Math” group require numeric-alike columns, meaning columns where is.numeric(x) || is.logical(x) || is.complex(x) is true. A data frame containing a character or factor column therefore cannot be passed whole to abs(). Select the numeric columns first, as in step 5 above (group generic manual page).
abs() on a factor or a character column
Factors and character strings are not numeric-alike. Convert the underlying values deliberately (for a factor, as.numeric(as.character(f))) before taking the absolute value, and check the conversion, because factor codes are not the original numbers.
Wrong result from a missing parentheses pair
Write abs(a - b), not abs(a) - b: the second form takes the absolute value of only the first term.
Free alternatives (table)
The absolute-value operation exists in other free analysis environments. The table compares the base R function with its NumPy counterpart.
| Tool | Function | Behaviour documented by the project |
|---|---|---|
| R (base) | abs(x) |
Numeric or complex vector or array; complex input returns the modulus, equal to Mod(z). |
| Python with NumPy | numpy.absolute(x) |
Element-wise absolute value; np.abs is a shorthand for it; for complex input a + ib the result is the square root of a² + b². See NumPy abs. |
If you are deciding between R and a menu-driven package for a whole analysis, see SPSS vs R.
FAQ
How do I get the absolute value in R?
Use abs(x). It is in base R, works element-wise on vectors, matrices and numeric data-frame columns, and needs no package.
What is the abs function in R for complex numbers?
For complex input abs(z) returns the modulus, identical to Mod(z), so abs(3+4i) is 5.
Can I take the absolute value of a whole data frame?
Only if every column is numeric, logical or complex. Otherwise apply abs to the numeric columns only, for example with mutate(across(where(is.numeric), abs)) in dplyr (dplyr across() reference).
Does abs() return an integer?
Yes when the input is integer or logical; otherwise it returns a double, per the R manual.
How do I compute the absolute difference between two columns?
Run abs(df$a - df$b); to average it, wrap it in mean().
Sources (official docs)
- R Core Team, base R manual: Miscellaneous Mathematical Functions (checked 2026-10-29).
- R Core Team, base R manual: S3 Group Generic Functions (checked 2026-10-29).
- R Core Team, base R manual: Complex Vectors (checked 2026-10-29).
- tidyverse: dplyr across() reference (checked 2026-10-29).
- NumPy: numpy.absolute (checked 2026-10-29).
- R distribution: CRAN (checked 2026-10-29).







