recipes (version 0.1.4)

roles: Manually Alter Roles

Description

add_role() adds a new role to an existing variable in the recipe. It does not overwrite old roles, as a single variable can have multiple roles.

update_role() alters an existing role in the recipe.

Usage

add_role(recipe, ..., new_role = "predictor", new_type = NULL)

update_role(recipe, ..., new_role = "predictor", old_role = NULL)

Arguments

recipe

An existing recipe().

...

One or more selector functions to choose which variables are being assigned a role. See selections() for more details.

new_role

A character string for a single role.

new_type

A character string for specific type that the variable should be identified as. If left as NULL, the type is automatically identified as the first type you see for that variable in summary(recipe).

old_role

A character string for the specific role to update for the variables selected by .... If left as NULL, all roles for each selected variable are updated to new_role. This is useful if you have a variable with multiple roles, and you want to alter only one of them.

Value

An updated recipe object.

Details

With add_role(), if a variable is selected that already has the new_role, a warning is emitted and that variable is skipped so no duplicate roles are added.

Adding or updating roles is a useful way to group certain variables that don't fall in the standard "predictor" bucket. You can perform a step on all of the variables that have a custom role with the selector has_role().

Examples

Run this code
# NOT RUN {
data(biomass)

# Create the recipe manually
rec <- recipe(x = biomass)
rec
summary(rec)

rec <- rec %>%
  update_role(carbon, contains("gen"), sulfur, new_role = "predictor") %>%
  update_role(sample, new_role = "id variable") %>%
  update_role(dataset, new_role = "splitting variable") %>%
  update_role(HHV, new_role = "outcome")

rec
summary(rec)

# Add a secondary role for carbon
rec <- rec %>%
  add_role(carbon, new_role = "carbon predictor")

summary(rec)

# Now update only the "predictor" role of carbon to instead
# be an additional outcome
rec %>%
  update_role(carbon, new_role = "outcome", old_role = "predictor") %>%
  summary()

# }

Run the code above in your browser using DataLab