Learn R Programming

mStats (version 3.4.0)

generate: Create a new variable

Description

generate() creates a new variable either by deriving from existing variables or with a constant value.

Usage

generate(data, var, expr = NULL)

Arguments

data

data.frame

var

name for the new variable

expr

a constant value, name of an existing variable or an expression for simple arithmetic or logical operations:

Value

data.frame with the new variable

Details

The values of the variable are specified by expr.

Label

The newly created variable is automatically labeled with the expression specified.

Examples

Run this code
# NOT RUN {
## generate variable with a constant value
generate(mtcars, new_var, NA)
generate(mtcars, new_var, 99)

## generate variable from an existing variable
generate(mtcars, new_var, mpg)

## generate variable with arithmetic operations
generate(iris, Length, Sepal.Length + Petal.Length)

# }

Run the code above in your browser using DataLab