Skip to main content
v2026.11,986 entries · CC-BY 4.0

Absolute Value in R: abs() Syntax and Data Frames

How to take the absolute value in R with abs(): vectors, data frames, complex numbers, return types and research uses such as absolute error.

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)), where abs() 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

  1. Single number. Run abs(-12). The result is 12.
  2. Vector. Run x <- c(-3, 2, 0, -1.5) and then abs(x). Each element is converted separately and the result is 3 2 0 1.5.
  3. Matrix. Run m <- matrix(c(-1, 2, -3, 4), nrow = 2) and then abs(m). The manual lists “a numeric or complex vector or array” as the argument, so the matrix keeps its shape.
  4. 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.
  5. Every numeric column. With dplyr, run library(dplyr) and then df |> mutate(across(where(is.numeric), abs)). In base R the equivalent is num <- sapply(df, is.numeric) followed by df[num] <- lapply(df[num], abs).
  6. 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, while abs(-3) is a double. Check with class() or typeof() if a later step depends on integer storage.
  • Complex numbers. For complex arguments, abs(z) == Mod(z). The modulus of z = x + yi is the square root of x² + y² (complex-number manual page), so abs(3+4i) returns 5.
  • Generic function. abs is an internal generic primitive: methods can be defined for individual classes or through the Math group 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)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Absolute Value in R: abs() Syntax and Data Frames

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →