Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

Transformation in R: transform(), mutate() and Log Transforms

Transformation in R: use transform(), mutate() and scale(), log transform right-skewed data, handle zeros with log1p(), and back-transform results.

Written and maintained by CASRAI Editorial Board

Last updated

Transformation in R means creating or changing columns of a data frame (or rescaling a matrix) with a function. Base R offers transform() and scale(), the tidyverse offers mutate(), and the usual statistical transformation for right-skewed data is the logarithm, log(). This page shows which tool to use, the exact calls from the official documentation, what happens to zeros and negative values, and how to reverse a transformation.

It is written for researchers who have a data frame in R and need to derive, rescale or log-transform variables before analysis. All function behaviour below is taken from the R reference manual and the dplyr reference, both checked on 2026-11-07.

What transformation is used for in research

Researchers transform variables to put them in comparable units (for example Fahrenheit to Celsius), to centre and scale predictors, or to reduce right skew before fitting a model. Because a transformation changes what the numbers mean, record it in your analysis plan and script so another person can reproduce it; see what data normalization is for the wider concept.

Install or access

transform(), scale(), log(), log1p() and exp() are part of base R, so they need no extra package. R itself is distributed through CRAN (cran.r-project.org). mutate() is in the dplyr package, documented at dplyr.tidyverse.org; install it once with install.packages("dplyr") and load it with library(dplyr).

Step by step: transform() in base R

transform() is a generic function with the usage transform(_data, ...). The R manual states that, at least currently, it only does anything useful with data frames. The ... arguments are tagged expressions evaluated within the data: a tag that matches an existing column name replaces that column, and any other tag is appended as a new column. The value returned is the modified data frame.

  1. Start with a data frame, here the built-in airquality.
  2. Replace a column: transform(airquality, Ozone = -Ozone).
  3. Add a new column and replace another in one call: transform(airquality, new = -Ozone, Temp = (Temp-32)/1.8). Here new is appended and Temp is overwritten with degrees Celsius.
  4. Assign the result to an object, because transform() returns a modified copy rather than changing the original.

The manual warns that transform() uses non-standard evaluation and is meant for interactive use; in programming code, use standard subsetting and arithmetic instead.

Transform data in R with mutate()

In dplyr, mutate(.data, ..., .by = NULL, .keep = c("all", "used", "unused", "none"), .before = NULL, .after = NULL) takes name-value pairs. Per the dplyr reference, later expressions can refer to columns created earlier in the same call, assigning NULL removes a column, .by applies the operation within groups, and across() applies one transformation to several columns, for example mutate(across(!name, as.factor)).

library(dplyr)
airquality |> mutate(Temp_C = (Temp - 32) / 1.8, .after = Temp)

If you use the pipe, see the pipe in R.

Log transform data in R (right-skewed data)

To log transform right-skewed data, apply log(x, base = exp(1)), log10(x), log2(x) or log1p(x). The manual defines log1p(x) as log(1+x) computed accurately even when |x| is much smaller than 1. Two edge cases matter for real data: log(0) gives -Inf and the log of a negative number gives NaN.

Worked example (illustrative)

The following uses the built-in airquality$Ozone column as an illustration of the workflow; it is not a claim about any specific study.

  1. Inspect the shape: hist(airquality$Ozone).
  2. Transform without producing infinities: airquality$log_ozone <- log1p(airquality$Ozone). Use log() instead when every value is strictly positive.
  3. Check the result before modelling: all(is.finite(na.omit(airquality$log_ozone))) should return TRUE. Missing values stay NA.
  4. Re-draw the histogram: hist(airquality$log_ozone), and compare it with the original.

For logarithmic axes in plots without changing the data, see ggplot log scale.

Scale and centre with scale()

scale(x, center = TRUE, scale = TRUE) works on a numeric matrix. With center = TRUE it subtracts column means (ignoring NA); with scale = TRUE it divides by the standard deviation when the data are centred, or by the root mean square otherwise. The result keeps the attributes "scaled:center" and "scaled:scale", which record what was applied and let you reverse it.

Output and how to read it

transform() and mutate() both return a data frame with the new or replaced columns; scale() returns a matrix with the two attributes above. Use names() or head() to confirm the columns, and is.finite() to detect -Inf, Inf and NaN after a log transform.

Back-transforming results

To return to the original scale, apply the inverse function: exp() reverses log() (and base^x reverses log10/log2), and expm1() reverses log1p(). For scale(), multiply by the stored "scaled:scale" and add "scaled:center". Report which scale each reported estimate is on.

Troubleshooting

  • -Inf in the output: the input contained zeros; use log1p() or document another handling choice.
  • NaN with a warning: the input contained negative values, for which log() is undefined.
  • transform() does nothing useful: the object is not a data frame.
  • Code works interactively but not inside a function: this is the non-standard evaluation warning in the transform() manual; switch to mutate() or standard subsetting.

transform() vs mutate() vs scale()

Tool Package Behaviour documented
transform() base R Tagged expressions replace or append columns; manual says it is meant for interactive use.
mutate() dplyr Supports .by grouping, .keep, .before/.after placement and across().
scale() base R Centres and scales matrix columns; keeps attributes that allow reversal.

FAQ

What is the transform function in R?

transform(_data, ...) is a base R generic that returns a modified data frame, replacing columns whose names match the tags and appending the rest.

How do I log transform data in R?

Apply log(), log10(), log2() or log1p() to the column, for example inside mutate() or transform(). Use log1p() when zeros are present.

Is there a PDF of the documentation?

The base R reference manual is published by the R Project; the help pages cited below are the same text available in R through ?transform, ?log and ?scale.

Sources

Follow CASRAI

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

Ask CASRAI · free to try

Ask about Transformation in R: transform(), mutate() and Log Transforms

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 →