Skip to content

Compare element-wise when value is a vector in chk_lte(), chk_lt(), chk_gte() and chk_gt() - #324

Merged
joethorley merged 2 commits into
mainfrom
joethorley/issue323
Oct 14, 2026
Merged

joethorley merged 2 commits into
mainfrom
joethorley/issue323

Conversation

@joethorley

@joethorley joethorley commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Closes #323

chk_lte(), chk_lt(), chk_gte() and chk_gt() (and their vld_ counterparts) now accept a value that is either:

  • a scalar: unchanged behaviour, or
  • a vector of the same length as x: values are compared element-wise and pairs where either value is missing are ignored. The error message gives the failing positions, e.g.
    `x` must have values less than or equal to the corresponding values of `value`, not at positions 1 and 2.

Any other length of value now errors with `value` must be a scalar or the same length as `x`. instead of being silently recycled.

start <- c(NA, 2, 1)
end <- c(0, 3, 1)
chk_lte(start, end) # previously failed (misaligned after dropping NA), now passes

Behaviour changes

  • A vector value whose length is not 1 or length(x) now errors instead of being recycled. value was documented as a scalar, so this only affects undocumented use. I haven't run reverse-dependency checks.
  • A scalar value behaves exactly as before, including a missing scalar value, for which vld_*() still returns NA.

Verification

  • New tests in tests/testthat/test-chk-range.R cover vector values for all four checkers, missing values on either side, length mismatches, POSIXct dates and error messages.
  • R CMD check: 0 errors, 0 warnings, 0 notes.

Performance

Per-call times for vld_lte(), old vs new (chk_lte() is similar). Each is the best of 5 timed loops.

Case Old New
Scalar x, scalar value 0.67 µs 1.42 µs
x of length 1e3, scalar value 5.7 µs 6.6 µs
x of length 1e6, scalar value 4.95 ms 4.75 ms
x of length 1e3, vector value 6.1 µs 4.7 µs
x of length 1e6, vector value 4.85 ms 2.85 ms
x of length 1e6 (10% NA), vector value errors 8.25 ms

A scalar value costs about 0.8 µs more per call, from the extra internal function call. When neither vector has missing values, the vectors are compared directly, which is faster than before because no subsetting is needed. Missing values in either vector are handled element-wise.

NEWS.md is left to fledge.

🤖 Generated with Claude Code

joethorley and others added 2 commits October 14, 2026 16:07
…hk_gte() and chk_gt()

Closes #323.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@joethorley
joethorley merged commit 9381246 into main Oct 14, 2026
11 checks passed
@joethorley
joethorley deleted the joethorley/issue323 branch October 14, 2026 23:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chk_lte(), chk_gte(), chk_lt() and chk_gt() silently misbehave when value is a vector

1 participant