rangemerge: Merge two tables by a range condition

View source: R/rangemerge.R

rangemergeR Documentation

Merge two tables by a range condition

Description

For each row of x, keep the row(s) of y whose value column falls between x's lower and upper bounds (inclusive), within the exact-match by key.

Usage

rangemerge(x, y, by, lower, upper, value)

Arguments

x, y

Data frames or data tables to join.

by

Character vector of exact-match join columns, present in both x and y.

lower, upper

Single columns in x describing the inclusive range bounds to test against.

value

Single column in y whose value is tested against each matching x row's ⁠[lower, upper]⁠ range.

Value

A basetable with one row per x row for each y row whose value falls within ⁠[lower, upper]⁠ (matching on by); an x row with no matching y row appears once with NA for y's columns.

Examples

x <- data.frame(id = c(1, 1, 2), lower = c(0, 0, 0), upper = c(10, 10, 20))
y <- data.frame(id = c(1, 1, 2), val = c(5, 15, 5), label = c("a", "b", "c"))
rangemerge(x, y, by = "id", lower = "lower", upper = "upper", value = "val")

basetable documentation built on Sept. 18, 2026, 9:13 a.m.