add: Add variables

Description Usage Arguments Details Value Author(s) Examples

View source: R/add.R

Description

Add new variables by mutating the input variables using a formula.

Usage

1
2
3
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 (right) or at the left (left) of the data in input.

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 0 or 1

...

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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))))

dformula documentation built on July 2, 2020, 3:37 a.m.