head()/tail() for emmGrid: follow base-R negative-n semantics#583
Open
SchmidtPaul wants to merge 1 commit into
Open
head()/tail() for emmGrid: follow base-R negative-n semantics#583SchmidtPaul wants to merge 1 commit into
head()/tail() for emmGrid: follow base-R negative-n semantics#583SchmidtPaul wants to merge 1 commit into
Conversation
For a negative n, base head(x, -k) drops the last k elements and tail(x, -k) drops the first k. The emmGrid methods had these swapped: head.emmGrid(x, -k) dropped the first k rows and tail.emmGrid(x, -k) dropped the last k. Rewrote both to compute the kept-row count the same way base R does (positive indexing only). Positive n is unchanged. Added a regression test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In base R,
head(x, -k)drops the last k elements andtail(x, -k)dropsthe first k. The
emmGridmethods have these swapped for a negativen:head.emmGrid(x, -k)drops the first k rows andtail.emmGrid(x, -k)drops thelast k. Positive
nis fine.Fix: rewrote both methods in
R/rbind.Rto compute the kept-row count exactlythe way
utils:::head.default()/tail.default()do —max(N + n, 0L)for anegative
n— using positive indexing only.n = 0andabs(n) > nrowreturnan empty grid, matching base behavior; positive
nis unchanged. Present sincethe
head()/tail()methods were added (commit 3a63e5d, 2022-08-30; firstshipped in 1.8.1); the help page has no negative-
nexample, so no documentedbehavior changes. The only internal callers use positive
n, so packageinternals are unaffected. Added a regression test in
tests/testthat/test-head-tail.R.Found via an AI-assisted bug hunt (see #580).