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

Richard O'Keefe r@oknz @end|ng |rom gm@||@com
Sat Sep 11 03:20:28 CEST 2021

```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.
> diff(x)
[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
> diff(x) != 0
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
> c(diff(x) != 0, TRUE)
That gives us a logical vector which we can use in indexing.
> w <- c(diff(x) != 0, TRUE)
> x[w] <- NA
> x
[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 using r-project.org> wrote:
>
> Hello List,
> Please look at the sample data frame below:
>
> ID         date1              date2             date3
> 1    2015-10-08    2015-12-17    2015-07-23
>
> 2    2016-01-16    NA                 2015-10-08
> 3    2016-08-01    NA                 2017-01-10
> 3    2017-01-10    NA                 2016-01-16
> 4    2016-01-19    2016-02-24   2016-08-01
> 5    2016-03-01    2016-03-10   2016-01-19
> This data frame was sorted by ID and date1. I need to set the column date3 as missing for the "last" record for each ID. In the sample data set, the ID 1, 2, 4 and 5 has one row only, so they can be consider as first and last records. the data3 can be set as missing. But the ID 3 has 2 rows. Since I sorted the data by ID and date1, the ID=3 and date1=2017-01-10 should be the last record only. I need to set date3=NA for this row only.
>
> the question is, how can I identify the "last" record and set it as NA in date3 column.
> Thank you,
> Kai
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

```