Skip to content

Clarify behavior of first() and last()#7056

Merged
tdhock merged 5 commits intoRdatatable:masterfrom
Mukulyadav2004:docs_clarity_first_last
Jun 11, 2025
Merged

Clarify behavior of first() and last()#7056
tdhock merged 5 commits intoRdatatable:masterfrom
Mukulyadav2004:docs_clarity_first_last

Conversation

@Mukulyadav2004
Copy link
Copy Markdown
Contributor

Closes #2002 and #2487

Adds a note to the documentation for first() and last() to clarify they are not direct replacements for base R subsetting (i.e., x[1] and x[length(x)]).

The note explains the different handling of zero-length vectors and the stripping of attributes. This helps prevent common user errors.

@tdhock @jangorecki @Anirban166 could you please review these docs update.

@codecov
Copy link
Copy Markdown

codecov bot commented Jun 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.69%. Comparing base (5bbc4d5) to head (27bef3e).
Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7056   +/-   ##
=======================================
  Coverage   98.69%   98.69%           
=======================================
  Files          79       79           
  Lines       14676    14676           
=======================================
  Hits        14485    14485           
  Misses        191      191           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread man/last.Rd Outdated
are passed through to \code{xts}'s first/last. }
}
\details{
Note: \code{first(x)} and \code{last(x)} are not exact replacements for \code{x[1]} and \code{x[length(x)]}. They differ in handling of zero-length vectors (returning a zero-length vector instead of \code{NA}) and do not preserve attributes like names.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good!
perhaps mention that this is intentionally similar to head(,1) ?

> x <- integer(0)
> first(x)
integer(0)
> head(x,1)
integer(0)
> x[1]
[1] NA

@Mukulyadav2004 Mukulyadav2004 requested a review from tdhock June 10, 2025 17:34
Comment thread man/last.Rd Outdated
}
\details{
Note: \code{first(x)} and \code{last(x)} are not exact replacements for \code{x[1]} and \code{x[length(x)]}. They differ in handling of zero-length vectors (returning a zero-length vector instead of \code{NA}) and do not preserve attributes like names.
Note: \code{first(x)} and \code{last(x)} are designed to behave like \code{head(x, 1)} and \code{tail(x, 1)}, respectively. Consequently, they differ from base R subsetting (\code{x[1]}): they return a zero-length vector for empty inputs (instead of \code{NA}) and do not preserve attributes like names.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way its read can imply that head() and tail() do not preserve attributes (since the current text emphasizes that first and last are designed to behave like them), when in fact they do, including names - The analogy holds for zero-length vectors only

Copy link
Copy Markdown
Contributor Author

@Mukulyadav2004 Mukulyadav2004 Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Does below this seems right to you? Can you please rephrase it with your suggestions.

first(x) and last(x) are not exact replacements for base R subsetting (e.g. x[1]). For zero-length vectors, they are designed to behave like head(x, 1) and tail(x, 1), returning an empty vector instead of NA. However, unlike both head(x, 1) and base subsetting, they do not preserve attributes like names.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Factually accurate, so I think what you wrote here sounds good!

I would just make it slightly more concise:
'For zero-length vectors, first(x) and last(x) mimic head(x, 1) and tail(x, 1) by returning an empty vector instead of NA. However, unlike head()/tail() and base R subsetting (e.g., x[1]), they do not preserve attributes like names.'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Avoid repetition wherever possible)

@tdhock tdhock merged commit e80a21b into Rdatatable:master Jun 11, 2025
10 checks passed
@tdhock
Copy link
Copy Markdown
Member

tdhock commented Jun 11, 2025

thanks!

@Mukulyadav2004 Mukulyadav2004 deleted the docs_clarity_first_last branch June 11, 2025 11:32
@Mukulyadav2004
Copy link
Copy Markdown
Contributor Author

Thanks @tdhock for merging the PR! Just wanted to say that I believe this also resolves the documentation point raised in issue #2487 .

Comment thread man/last.Rd
are passed through to \code{xts}'s first/last. }
}
\details{
Note: For zero-length vectors, \code{first(x)} and \code{last(x)} mimic \code{head(x, 1)} and \code{tail(x, 1)} by returning an empty vector instead of \code{NA}. However, unlike \code{head()}/\code{tail()} and base R subsetting (e.g., \code{x[1]}), they do not preserve attributes like names.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a Rd keyword note that seems more feasible here rather than details, considering sentence starts with "Note:".
example:

\note{ Current implementation does not make use of sorted keys. }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! so do I have to submit a new PR?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

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.

first(x) # [not always the] same as x[1]

4 participants