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

transform() in R: Syntax, Examples and When Not to Use It

How the R transform() function adds and replaces data frame columns, with the manual’s examples and a table comparing within(), mutate() and $ assignment.

Written and maintained by CASRAI Editorial Board

Last updated

transform() is a base R function that adds or replaces columns in a data frame using expressions written in terms of the columns already there. The R manual calls it “a convenience function intended for use interactively”, and says that for programming the standard subsetting and arithmetic functions are the better choice. This guide shows the exact syntax from the manual, a worked example on the built-in airquality data, and a side-by-side comparison with within(), dplyr::mutate() and plain $ assignment, so you can pick the right one for a script you intend to rerun.

Note that “transform” here means the R function transform(), not a statistical data transformation such as a log or Box-Cox transform. For the statistical side, see the log transformation formula guide and log and other data transformations.

What transform() in R is used for in research

Research datasets usually arrive with columns in the wrong units or without the derived variable an analysis needs. transform() is a short way to do that at the console: convert a unit, flip a sign, or add a log-scaled copy of a measurement. It belongs to the family of data-preparation steps described in data cleaning steps. For another way to reshape and summarise data in R, see collapse in R.

What the R transform function returns

According to the manual, the value of transform() is “the modified value of _data“. The call returns the modified data frame; assign the result to a name if you want to keep it.

Install or access

transform() is part of base R, so there is nothing extra to install beyond R itself. Download R from the Comprehensive R Archive Network at cran.r-project.org. The function’s reference page is in the R manual: stat.ethz.ch/R-manual/R-devel/library/base/html/transform.html (checked 2026-10-29). From an R session, ?transform opens the same help page.

Step-by-step: add and change columns with transform()

The syntax in the manual is transform(_data, ...). The first argument, _data, is “the object to be transformed”. The remaining arguments are “further arguments of the form tag=value“. The manual’s details section says these are tagged vector expressions “evaluated in the data frame _data“, that tags matching a name in names(_data) replace that column, and that the other tags are appended to the data frame.

  1. Pick a data frame. The manual’s examples use the built-in airquality data frame.
  2. Replace an existing column. Use a tag with the same name as the column:
    transform(airquality, Ozone = -Ozone)
  3. Add a new column and replace one in the same call. A tag with a new name is appended; a tag named after an existing column replaces it. This is the manual’s Fahrenheit-to-Celsius example:
    transform(airquality, new = -Ozone, Temp = (Temp-32)/1.8)
  4. Keep the result. Assign it, for example aq2 <- transform(airquality, TempC = (Temp - 32) / 1.8). Because the tag TempC does not match an existing column name, it is appended as a new column and Temp is left unchanged.
  5. Add a log-scaled column. The manual’s last example adds logOzone = log(Ozone) to the Ozone values after attach(airquality) and before detach(airquality). The same idea written against the data frame is transform(airquality, logOzone = log(Ozone)).

Output and how to read it

The result is the modified data frame. Replaced columns keep their names; appended columns are added to the data frame, per the manual. Check the result with head() and names() before building on it: confirm that the new column names are the ones you intended and that no existing column was overwritten by a tag that shares its name. The manual’s note on lengths applies to every tag: “if some of the values are not vectors of the appropriate length, you deserve whatever you get!”

Troubleshooting

A column changed that you meant to keep

Any tag whose name matches a column replaces that column. If you wanted a new column, give the tag a new name.

Your script behaves oddly when column names are variables

The manual’s warning says the non-standard evaluation of the argument “can have unanticipated consequences” when programming, and recommends the standard subsetting and arithmetic functions instead. In a script or function, assign columns directly (df$TempC <- (df$Temp - 32) / 1.8) rather than relying on transform().

The new column has the wrong number of values

Each tag must evaluate to a vector of the right length for the data frame. Check nrow() against the length of your expression.

transform() vs within(), dplyr mutate() and $ assignment

These are the common ways to create or change columns in R. The table lists only what the cited documentation states.

Approach What the documentation says Source
transform(_data, ...) Tagged expressions are evaluated in the data frame; matching tags replace columns, others are appended. Described as a convenience function intended for interactive use. R manual, transform
within(data, expr, ...) Similar to with(), except that it examines the environment after the evaluation of expr. R manual, with
dplyr::mutate(.data, ...) Creates new columns that are functions of existing variables; newly created variables are available immediately; .before and .after control where new columns go, and .keep controls which columns are retained. dplyr reference, mutate
Standard subsetting and arithmetic ($<-, [) Recommended by the transform manual for programming. R manual, transform

The transform manual itself lists within() under See Also as “a more flexible approach”. The practical difference the sources support: transform() is for quick interactive edits, while the manual points programmers to standard subsetting. If you already use the tidyverse, mutate() adds control over column placement and over which columns are kept, which transform() does not document. Whichever you pick, keep the code in a script so the step can be rerun, which is the core of computational reproducibility.

FAQ

What does the transform function in R do?

It returns a modified copy of a data frame in which each tag=value argument either replaces the column of the same name or is appended as a new column.

Is transform() in R the same as a log transformation?

No. transform() is a data-frame function; a log transformation is a statistical operation you can carry out inside it, for example logOzone = log(Ozone) as in the manual’s example.

Should I use transform() or mutate()?

The R manual describes transform() as intended for interactive use and recommends standard subsetting for programming. mutate() is the dplyr function for creating columns and it makes newly created variables available immediately.

Can transform() change several columns at once?

Yes. Pass several tag=value arguments, as in the manual’s example that adds new and replaces Temp in one call.

Sources

  • R Core Team, R manual, base package, transform, checked 2026-10-29.
  • R Core Team, R manual, base package, with and within, checked 2026-10-29.
  • tidyverse, dplyr reference, mutate, checked 2026-10-29.
  • The Comprehensive R Archive Network, cran.r-project.org, checked 2026-10-29.

Follow CASRAI

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

Ask CASRAI · free to try

Ask about transform() in R: Syntax, Examples and When Not to Use It

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 →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.