left_join_checks: Left Join with Validation Checks

View source: R/left_join_checks.R

left_join_checksR Documentation

Left Join with Validation Checks

Description

a custom usage of left_join, with more detailed checks. Performs a left join and verifies that no unexpected duplicates or mismatches occur. In cas of unexpected results, gives details about what caused the problem.

Usage

left_join_checks(
  x,
  y,
  ...,
  req_xAllMatch = 1,
  req_preserved_x = 1,
  behavior = "error",
  showNotFound = FALSE,
  showProblems = TRUE,
  time = FALSE
)

Arguments

x

A data.table representing the left table.

y

A data.table representing the right table.

...

Additional arguments passed to 'dplyr::left_join'.

req_xAllMatch

Logical. Ensure that all rows in 'x' find a match in 'y'. Default: FALSE.

req_preserved_x

Logical. Ensure that the number of rows in 'x' remains unchanged after the join. Default: TRUE.

behavior

Character. Specifies behavior if validation fails. Options: '"warning"' or '"error"'. (default: '"warning"')

showNotFound

Logical. Show rows from 'x' that did not match with 'y'. Default: FALSE.

showProblems

Logical. Display the problems encountered during the joining process, if any.

time

Logical. Internal argument used only for testing purposes, timing the function steps

Value

A data.table containing the joined table.

Examples

library(data.table)
library(dplyr)

# Example 1: Simple left join with all matches
table_left <- data.table(id = 1:3, value_left = c("A", "B", "C"))
table_right <- data.table(id = 1:3, value_right = c("X", "Y", "Z"))
result <- left_join_checks(table_left, table_right, by = "id", req_preserved_x = TRUE)
print(result) # Ensures all rows in table_left are preserved

# Example 2: Left join with missing matches
table_left <- data.table(id = 1:5, value_left = c("A", "B", "C", "D", "E"))
table_right <- data.table(id = c(1, 3, 5), value_right = c("X", "Y", "Z"))
result <- left_join_checks(
  table_left,
  table_right,
  by = "id",
  req_preserved_x = TRUE,
  showNotFound = TRUE,
  behavior = "warning"
)
print(result) # Rows from table_left with no matches in table_right are shown


R.AlphA.Home documentation built on April 3, 2025, 5:53 p.m.