H17 Look up brokers
Use your current course session. Preparation and course runner · R script ↓
H17 Look up brokers
Goal: Add a second attribute.
Prerequisites: H16.
Extend the same recipe approach and revise the output contract.
training_step(
"H17",
{
brokers <- data.frame(
broker_id = 1:2,
broker_name = c("Broker A", "Broker B")
)
enriched_contract <- dr_contract_update(
linked_contract,
version = "2.0.0",
columns = c(broker_name = "character")
)
enrichment_recipe <- customer_recipe |>
dr_step_lookup(brokers, by = "broker_id")
enriched_product <- dr_product(
"policies.enriched",
linked_policies,
contract = enriched_contract
) |>
dr_add_recipe(enrichment_recipe)
enriched_rows <- dr_collect(dr_run(enriched_product, write = FALSE))
print(enriched_rows)
stopifnot(nrow(enriched_rows) == 6L, !anyNA(enriched_rows$broker_name))
},
needs = c("H16")
)Expected result: Broker names without multiplying rows.
Task: Why is a duplicate broker_id in brokers a problem?
Show solution
It could duplicate a policy. The checked lookup protects this relationship.