Skip to content
Prev 389123 / 398506 Next

how to find "first" or "last" record after sort in R

Let's simplify this to consider a single vector, such as
x <- c(1,1,1,2,2,3,3,3,3,4,5,5,5)
in which equal elements are in contiguous blocks.
[1] 0 0 1 0 1 0 0 0 1 1 0 0
Of course, there could be gaps, or the sequence might be descending
instead of ascending.  So
We are nearly there, but there is a problem.
The last element of the vector is always the last element of a group,
but it will never be reported, because there is no following element
to compare it with.  So
That gives us a logical vector which we can use in indexing.
[1]  1  1 NA  2 NA  3  3  3 NA NA  5  5 NA
On Fri, 10 Sept 2021 at 07:00, Kai Yang via R-help <r-help at r-project.org> wrote: