Performs a join operation that fails if the specified cardinality constraint is violated. Use this to catch unexpected many-to-many relationships early.
Arguments
- x
A data frame (left table).
- y
A data frame (right table).
- by
A character vector of column names to join by.
- type
Character. The type of join to perform. One of
"left"(default),"right","inner","full".- expect
Character. The expected cardinality relationship. One of:
- "1:1"
Each key in x matches at most one key in y, and vice versa
- "1:m" or "1:many"
Each key in x can match multiple keys in y, but each key in y matches at most one key in x
- "m:1" or "many:1"
Each key in y can match multiple keys in x, but each key in x matches at most one key in y
- "m:m" or "many:many"
No cardinality constraints (allows all relationships)
- ...
Additional arguments passed to the underlying join function.
Value
The joined data frame if the cardinality constraint is satisfied. Throws an error if the constraint is violated.
Examples
orders <- data.frame(id = 1:3, product = c("A", "B", "C"))
customers <- data.frame(id = 1:3, name = c("Alice", "Bob", "Carol"))
# This succeeds (1:1 relationship)
join_strict(orders, customers, by = "id", expect = "1:1")
# This would fail if customers had duplicate ids
if (FALSE) { # \dontrun{
customers_dup <- data.frame(id = c(1, 1, 2), name = c("A1", "A2", "B"))
join_strict(orders, customers_dup, by = "id", expect = "1:1")
# Error: Expected 1:1 relationship but found 1:many
} # }