Select row with most recent date by group
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)
SQL Group By most recent date and sales value
SELECT ID, Name, Order, Date FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Date DESC) AS sn
FROM your_table_name
) A WHERE sn = 1;
Select row with most recent date per location and increment recent date by 1 for each row by location using MariaDB
You could use analytic window functions and update the original table by joining to a sub-query (works for MariaDB):
update t
join (
select Id,
Date_Add(First_Value(date) over(partition by locationId order by date desc),
interval (13 + row_number() over(partition by locationId order by date desc)) day
) NewDate
from t
)nd on t.id = nd.id
set t.Newdate = nd.NewDate;
See DB<>Fiddle example
Keeping only rows with most recent date in dataframe
This can be done by sort_values
& drop_duplicates
:
df = df.sort_values(by=['Modified Date'], ascending=False)
df = drop_duplicates(subset='School ID', keep='first)
Where the sort ensures that for each school the newest date will appear first, and the drop duplicates takes the first appearance of each school, which is the newest.
Related Topics
How to Define More Line Types for Graphs in R (Custom Linetype)
How to Maintain Size of Ggplot with Long Labels
Ggplot2 Multiple Scales/Legends Per Aesthetic, Revisited
Stop an R Program Without Error
Improve Centering County Names Ggplot & Maps
How to Implement a Cleanup Routine in R Shiny
Read and Rbind Multiple CSV Files
R Fuzzy String Match to Return Specific Column Based on Matched String
Ggplot for Loop Outputs All the Same Graph
How to Delete Groups Containing Less Than 3 Rows of Data in R
Calculate Correlation with Cor(), Only for Numerical Columns
R Scatter Plot: Symbol Color Represents Number of Overlapping Points
How to Add a Factor Column to Dataframe Based on a Conditional Statement from Another Column
Plots Generated by 'Plot' and 'Ggplot' Side-By-Side
Date Format in Tooltip of Ggplotly
Outputting Multiple Lines of Text with Rendertext() in R Shiny