Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select row with most recent date by group

Tags:

r

I have a data frame in R where the rows represent events, and one column is the date of the event. The thing the event is happening to is described by an ID column. So for each ID there are multiple entries.

How do I filter the data frame so that I retain only the most recent event for each ID? The IDs are integers and the dates are in the form mm/dd/yyyy.

like image 791
Ben S. Avatar asked May 05 '15 16:05

Ben S.


People also ask

How do I select the last 3 rows in SQL?

Try only this:- SELECT * FROM reset ORDER BY ASC LIMIT (FOUND_ROWS() - 3), 3 and check if it is giving the last 3 rows from your table in ascending order!!!

How do I get the latest date in PostgreSQL?

The PostgreSQL CURRENT_DATE function returns the current date (the system date on the machine running PostgreSQL) as a value in the 'YYYY-MM-DD' format.


1 Answers

You can try

library(dplyr)
df %>% 
  group_by(ID) %>%
  slice(which.max(as.Date(date, '%m/%d/%Y')))

data

df <- data.frame(ID= rep(1:3, each=3), date=c('02/20/1989',
'03/14/2001', '02/25/1990',  '04/20/2002', '02/04/2005', '02/01/2008',
'08/22/2011','08/20/2009', '08/25/2010' ), stringsAsFactors=FALSE)
like image 148
akrun Avatar answered Oct 07 '22 08:10

akrun