
Last chance! 50% off unlimited learning
Sale ends in
ftransform
is a much faster version of base::transform
and dplyr::mutate
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.
# Modify and return data frame
ftransform(X, …)
ftransformv(X, vars, FUN, …, apply = TRUE)
tfm(X, …) # Shortcut for ftransform
tfmv(X, vars, FUN, …, apply = TRUE)# Modify data frame by reference
settransform(X, …)
settransformv(X, vars, FUN, …, apply = TRUE)
settfm(X, …) # Shortcut for settransform
settfmv(X, vars, FUN, …, apply = TRUE)
# Replace/add modified columns in/to a data frame
ftransform(X) <- value
tfm(X) <- value # Shortcut for ftransform<-
# Compute columns, returned as a new data frame
fcompute(X, …)
a data frame or named list of columns.
further arguments of the form column = value
. The value
can be a combination of other columns, a scalar value, or NULL
, which deletes column
. Alternatively it is also possible to place a single list here, which will be treated like a list of column = value
arguments. For ftransformv
, …
can be used to pass further arguments to FUN
. Note: The ellipsis (…
) is always evaluated within the data frame (X
) environment. See Examples.
variables to be transformed by applying FUN
to them: select using names, indices, a logical vector or a selector function (e.g. is.numeric
).
a single function yielding a result of length NROW(X)
or 1. See also apply
.
logical. TRUE
(default) will apply FUN
to each column selected in vars
; FALSE
will apply FUN
to the subsetted data frame i.e. FUN(get_vars(X, vars), ...)
. The latter is useful for collapse functions with data frame or grouped tibble / panel-data frame methods, yielding performance gains and enabling grouped transformations. See Examples.
a named list of replacements, it will be treated like an evaluated list of column = value
arguments.
The modified data frame X
, or, for fcompute
, a new data frame with the columns computed on X
. All attributes of X
are preserved.
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 values replace the corresponding variable in
X
, whereas the others are appended to X
. It is also possible to delete columns by assigning NULL
to them, i.e. ftransform(data, colk = NULL)
removes colk
from the data. Note that names(X)
and the names of the ...
arguments are checked for uniqueness beforehand, yielding an error if this is not the case.
Since collapse v1.3.0, is is also possible to pass a single named list to …
, i.e. ftransform(data, newdata)
. This list will be treated like a list of tagged vector expressions. Note the different behavior: ftransform(data, list(newcol = col1))
is the same as ftransform(data, newcol = col1)
, whereas ftransform(data, newcol = as.list(col1))
creates a list column. Something like ftransform(data, as.list(col1))
gives an error because the list is not named. See Examples.
The function ftransformv
added in v1.3.2 provides a fast replacement for the functions dplyr::mutate_at
and dplyr::mutate_if
facilitating mutations of groups of columns (dplyr::mutate_all
is already accounted for by dapply
). See Examples.
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 afterwards).
The function fcompute
works just like ftransform
, but returns only the changed / computed columns without modifying or appending the data in X
.
with
, within
, add_vars
, Data Frame Manipulation, Collapse Overview
# NOT RUN {
## ftransform modifies and returns a data.frame
head(ftransform(airquality, Ozone = -Ozone))
head(ftransform(airquality, new = -Ozone, Temp = (Temp-32)/1.8))
head(ftransform(airquality, new = -Ozone, new2 = 1, Temp = NULL)) # Deleting Temp
head(ftransform(airquality, Ozone = NULL, Temp = NULL)) # Deleting columns
# With collapse's grouped and weighted functions, complex operations are done on the fly
head(ftransform(airquality, # Grouped operations by month:
Ozone_Month_median = fmedian(Ozone, Month, TRA = "replace_fill"),
Ozone_Month_sd = fsd(Ozone, Month, TRA = "replace"),
Ozone_Month_centered = fwithin(Ozone, Month)))
# Grouping by month and above/below average temperature in each month
head(ftransform(airquality, Ozone_Month_high_median =
fmedian(Ozone, list(Month, Temp > fbetween(Temp, Month)), TRA = "replace_fill")))
## ftransformv can be used to modify multiply columns using a function
head(ftransformv(airquality, 1:3, log))
head(`[<-`(airquality, 1:3, value = lapply(airquality[1:3], log))) # Same thing in base R
head(ftransformv(airquality, 1:3, log, apply = FALSE))
head(`[<-`(airquality, 1:3, value = log(airquality[1:3]))) # Same thing in base R
# Using apply = FALSE yields meaningful performance gains with collapse functions
# This calls fwithin.default, and repeates the grouping by month 3 times:
head(ftransformv(airquality, 1:3, fwithin, Month))
# This calls fwithin.data.frame, and only groups one time -> 5x faster!
head(ftransformv(airquality, 1:3, fwithin, Month, apply = FALSE))
library(magrittr) # Pipe operators
# This also works for grouped and panel data frames (calling fwithin.grouped_df)
airquality %>% fgroup_by(Month) %>%
ftransformv(1:3, fwithin, apply = FALSE) %>% head
# But this gives the WRONG result (calling fwithin.default). Need option apply = FALSE!!
airquality %>% fgroup_by(Month) %>%
ftransformv(1:3, fwithin) %>% head
## For more complex mutations we can use ftransform with compound pipes
airquality %>% fgroup_by(Month) %>%
ftransform(get_vars(., 1:3) %>% fwithin %>% flag(0:2)) %>% head
airquality %>% ftransform(STD(., cols = 1:3) %>% replace_NA(0)) %>% head
# The list argument feature also allows flexible operations creating multiple new columns
airquality %>% # The variance of Wind and Ozone, by month, weighted by temperature:
ftransform(fvar(list(Wind_var = Wind, Ozone_var = Ozone), Month, Temp, "replace")) %>% head
# Same as above using a grouped tibble (a bit more complex)
airquality %>% fgroup_by(Month) %>%
ftransform(fselect(., Wind, Ozone) %>% fvar(Temp, "replace") %>% add_stub("_var", FALSE)) %>%
fungroup %>% head
# This performs 2 different multi-column grouped operations (need c() to make it one list)
ftransform(airquality, c(fmedian(list(Wind_Day_median = Wind,
Ozone_Day_median = Ozone), Day, TRA = "replace"),
fsd(list(Wind_Month_sd = Wind,
Ozone_Month_sd = Ozone), Month, TRA = "replace"))) %>% head
## settransform(v) works like ftransform(v) but modifies a data frame in the global environment..
settransform(airquality, Ratio = Ozone / Temp, Ozone = NULL, Temp = NULL)
head(airquality)
rm(airquality)
# Grouped and weighted centering
settransformv(airquality, 1:3, fwithin, Month, Temp, apply = FALSE)
head(airquality)
rm(airquality)
# Suitably lagged first-differences
settransform(airquality, get_vars(airquality, 1:3) %>% fdiff %>% flag(0:2))
head(airquality)
rm(airquality)
# Same as above using magrittr::`%<>%`
airquality %<>% ftransform(get_vars(., 1:3) %>% fdiff %>% flag(0:2))
head(airquality)
rm(airquality)
# It is also possible to achieve the same thing via a replacement method (if needed)
ftransform(airquality) <- get_vars(airquality, 1:3) %>% fdiff %>% flag(0:2)
head(airquality)
rm(airquality)
## fcompute only returns the modified / computed columns, ...
head(fcompute(airquality, Ozone = -Ozone))
head(fcompute(airquality, new = -Ozone, Temp = (Temp-32)/1.8))
head(fcompute(airquality, new = -Ozone, new2 = 1))
# }
Run the code above in your browser using DataLab