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

2021-09-10 Thread Richard O'Keefe
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 instead of ascending. So > diff(x) != 0 We are

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

2021-09-10 Thread Avi Gross via R-help
-help Subject: Re: [R] how to find "first" or "last" record after sort in R I prefer the duplicated() function, since the final code will be clear to a future reader. (Particularly when I am that future reader). last <- !duplicated(mydata$ID, fromLast=TRUE) # point

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

2021-09-10 Thread Kai Yang via R-help
Thanks Therneau, duplicated() function works well. --- Kai On Friday, September 10, 2021, 05:13:47 AM PDT, Therneau, Terry M., Ph.D. wrote: I prefer the duplicated() function, since the final code will be clear to a future reader.   (Particularly when I am that future reader). last

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

2021-09-10 Thread Therneau, Terry M., Ph.D. via R-help
I prefer the duplicated() function, since the final code will be clear to a future reader. (Particularly when I am that future reader). last <- !duplicated(mydata$ID, fromLast=TRUE) # point to the last ID for each subject mydata$data3[last] <- NA Terry T. (I read the list once a day in

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

2021-09-09 Thread Avi Gross via R-help
then applying some functionality to each smaller data.frame to get the result then recombining it back. -Original Message- From: R-help On Behalf Of Kai Yang via R-help Sent: Thursday, September 9, 2021 3:00 PM To: R-help Mailing List Subject: [R] how to find "first" or "

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

2021-09-09 Thread Bert Gunter
Sorry, that should be > id <- c(1,2,2,2,3,4,5,5) > last.index <- cumsum(rle(id)$lengths) > last.index [1] 1 4 5 6 8 of course. Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom

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

2021-09-09 Thread Bert Gunter
Many ways to do this, of course, but if I understand correctly ?rle may be the simplest, because you already have the data sorted by ID. The following little example should give you the idea. It gets the index of the last row in each id,, which you can then use to assign NA's or whatever: > id

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

2021-09-09 Thread Kai Yang via R-help
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