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
在基地:
tail(unique(df[order(df$Week),]),4)
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.