Learn R Programming

collapse (version 1.2.1)

ftransform: Fast Transform and Compute Columns on a Data Frame

Description

ftransform is a much faster update of base::transform for data frames. It returns the data frame with new columns computed and/or existing columns modified or deleted. settransform does all of that by reference i.e. it modifies the data frame in the global environment. fcompute can be used to compute new columns from the columns in a data frame and returns only the computed columns.

Usage

# Modify and return 'data.frame'
ftransform(X, ...)
tfm(X, ...)            # Shortcut for ftransform

# Modify 'data.frame' by reference settransform(X, ...) settfm(X, ...) # Shortcut for settransform

# Compute and return new 'data.frame' from existing one fcompute(X, ...)

Arguments

X

a data.frame.

further arguments of the form column = value. The value can be a combination of other columns, a scalar value, or NULL, which deletes column.

Value

The modified data.frame X, or, for fcompute, a new data.frame with the columns computed on X. All attributes of X are preserved.

Details

The arguments to ftransform are tagged vector expressions, which are evaluated in the data frame X. The tags are matched against names(X), and for those that match, the value replace the corresponding variable in X, and the others are appended to X. It is also possible to delete columns by assigning NULL to them, i.e. ftransform(data, column = NULL) removes column from the data.

The function settransform does all of that by reference, but uses base-R's copy-on modify semantics, which is equivalent to replacing the data with <- (thus it is still memory efficient but the data will have a different memory address after each call of settransform).

Finally, the function fcompute functions just like ftransform, but returns only the changed / computed columns without modifying or appending the data in X.

See Also

with, within, Data Frame Manipulation, Collapse Overview

Examples

Run this code
# NOT RUN {
## ftransform modifies and returns a data.frame
ftransform(airquality, Ozone = -Ozone)
ftransform(airquality, new = -Ozone, Temp = (Temp-32)/1.8)
ftransform(airquality, new = -Ozone, new2 = 1, Temp = NULL)  # Deleting Temp
ftransform(airquality, Ozone = NULL, Temp = NULL)            # Deleting columns

## settransform modifies a data.frame in the global environment
airquality_c <- airquality
settransform(airquality_c, Ratio = Ozone / Temp, Ozone = NULL, Temp = NULL)
head(airquality_c)
rm(airquality_c)

## fcompute only returns the modified / computed data
fcompute(airquality, Ozone = -Ozone)
fcompute(airquality, new = -Ozone, Temp = (Temp-32)/1.8)
fcompute(airquality, new = -Ozone, new2 = 1)

# }

Run the code above in your browser using DataLab