add {dformula} | R Documentation |
Add variables
Description
Add new variables by mutating the input variables using a formula.
Usage
add(from, formula, as = NULL,
position = c("right", "left"),
na.remove = FALSE, logic_convert = TRUE,...)
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. |
position |
if the new varaibles are positioned at the begining ( |
na.remove |
a logical value indicating whether NA values should be removed. |
logic_convert |
logical value indicating if the new logical varaible are convertet to |
... |
further arguments |
Details
The formula is composed of two part:
~ new_variables
the right-hand are the new varaible to add starting from the existing varaibles, using the I()
function.
For example:
~ I(log(column_names1)) + I(column_names2/100)
the column_names1
and log(column_names1)
are added to the data.
If na.remove
is set ti TRUE
, new variables are created, added to the dataset in input and then the observation with missing are removed.
Value
Returns a data.frame object with the original and the new varaibles.
Author(s)
Alessio Serafini
Examples
data("airquality")
dt <- airquality
head(add(from = dt, formula = ~ log(Ozone)))
head(add(from = dt, formula = ~ log(Ozone) + log(Wind)))
head(add(from = dt, formula = ~ log(Ozone), as = "Ozone_1"))
head(add(from = dt, formula = Ozone + Wind ~ log()))
head(add(from = dt, formula = ~ log()))
head(add(from = dt, formula = .~ log(), position = "left"))
head(add(from = dt, formula = .~ log(), na.remove = TRUE))
head(add(from = dt, formula = ~ I((Ozone>5))))
head(add(from = dt, formula = ~ I((Ozone>5)), logic_convert = FALSE ))
head(add(from = dt, formula = Ozone + Wind ~ C(Ozone-Ozone)))
head(add(from = dt, formula = ~ C(log(Ozone))))
head(add(from = dt, formula = ~ C(5)))
head(add(from = dt, formula = Ozone + Wind ~ C(Ozone-Ozone)))
head(add(from = dt, formula = Ozone + Wind ~ C(log(Ozone))))
foo <- function(x, a = 100){return(x-x + a)}
head(add(from = dt, formula = Ozone + Month~ I(foo(a = 100))))
head(add(from = dt, formula = Ozone + Month~ foo()))
head(add(from = dt, formula = ~ I(foo(Ozone, a = 100))))