Learn R Programming

dformula (version 1.0)

transform: Transform varibles

Description

Mutate input variables using a formula.

Usage

transform(from, formula, as = NULL,
          na.remove = FALSE, logic_convert = TRUE, ...)

Value

Returns the original data.frame object with mutaded varaibles.

Arguments

from

a data.frame object with variables

formula

a formula indicating the operation to create new varibles. Look at the detail section for explanantion.

as

a character vector with names of new variables.

na.remove

a logical value indicating whether NA values should be removed.

logic_convert

logical value indicating if the new logical varaible are converted to 0 or 1

...

further arguments

Author

Alessio Serafini

Details

The formula is composed of two part:

column_names ~ trasformed_variables

the left-hand side are the names of the column to transform, and the right-hand the operations applied to the selected columns, using the I() function.

For example:

column_names1 + column_names2 ~ I(log(column_names1)) + I(column_names2/100)

the column_names1 is mutated in log(column_names1) and column_names2 is divided by 100.

If na.remove is set to TRUE, variables are mutaded, and then the observation with missing are removed.

Examples

Run this code

data("airquality")
dt <- airquality

head(transform(from = dt, Ozone ~ I(Ozone-Ozone)))
head(transform(from = dt, Ozone ~ log(Ozone)))
head(transform(from = dt, Ozone ~ I(Ozone>5)))
head(transform(from = dt, Ozone ~ I(Ozone>5), logic_convert = TRUE))


head(transform(from = dt,  ~ log()))
head(transform(from = dt, . ~ log()))
head(transform(from = dt, NULL ~ log()))

head(transform(from = dt, Ozone + Day ~ log()))
head(transform(from = dt, Ozone + Day ~ log(Ozone/100) + exp(Day)))
head(transform(from = dt, Ozone ~ log()))

head(transform(from = dt,Ozone + Wind ~ C(log(1))))
head(transform(from = dt,Ozone + Wind ~ log(Ozone) + C(10)))


head(transform(from = dt, Ozone + Wind~ C(log(Ozone))))


foo <- function(x, a = 100){return(x-x + a)}
head(transform(from = dt, Ozone + Wind ~ foo(a = 100)))
head(transform(from = dt, . ~ foo(a = 100)))

head(transform(from = dt, Ozone + Wind ~ log(log(1))))

Run the code above in your browser using DataLab