Home > OS >  4 most recent dates in R
4 most recent dates in R

Time:06-16

I have a data frame with a column of dates and I want to filter the rows to only include the 4 dates closest to today. So in the below data set i would only want 6/5/2022,5/29/2022,5/22/2022,and 5/15/2022 since those are the latest 4 dates.

Week    State   Actuals
5/19/2019   -   1
2/2/2020    -   1
5/17/2020   CA  1
6/14/2020   CA  1
9/27/2020   CA  1
4/10/2022   TX  1
4/17/2022   CA  3
4/24/2022   FL  1
4/24/2022   NV  1
4/24/2022   CA  55
4/24/2022   TX  2
5/1/2022    CA  29
5/1/2022    FL  1
5/1/2022    TX  3
5/8/2022    NV  2
5/8/2022    TX  3
5/8/2022    CA  34
5/8/2022    AZ  1
5/15/2022   TX  1
5/15/2022   CA  27
5/22/2022   CA  15
5/29/2022   TX  2
5/29/2022   CA  27
6/5/2022    TX  1
6/5/2022    CA  23

CodePudding user response:

in base:

tail(unique(df[order(df$Week),]),4)

CodePudding user response:

A possible solution, based on dplyr and lubridate::mdy:

library(dplyr)
library(lubridate)


df %>%
  filter(!duplicated(Week)) %>% 
  slice_max(mdy(Week), n = 4)

#>        Week State Actuals
#> 1  6/5/2022    TX       1
#> 2 5/29/2022    TX       2
#> 3 5/22/2022    CA      15
#> 4 5/15/2022    TX       1
  • Related