filtering data by specific date year over year

Hello Everyone,

Thank you for the time you have taken to read this post. I have a brief questions to has puzzled me over the last few days:

I have a data set that analyzes athlete body weight dates that occur every week over years and although I need all of this data from some aspects of the R Shiny dashboard I am creating, In other aspects of my dashboard I am only interested in the current season's data. Here I am defining that the season begins each year on September 1.

I do not want to filter the data by September 1 for a specific year, but rather I would like to be able to filter where R will provide only the data from the most recent September 1 to the current date.

I hope this makes sense, I have provided a brief reproducible data frame below. If you would like any other information please let me know. Your help is much appreciated


 Weight <- c("100","97","98","100","110","105","100","101","99","110")
 Dates <-  c("2019-04-20","2019-05-10","2019-08-10","2020-09-01","2020-09-20","2020-10-20","2021-01-07","2021-01-20","2021-03-10")

 df  <- data.frame(Dates, Weight)


In this situation, I would like to filter the dates from September 1, 2020 to the current date. Keeping in mind I would like this filter to automatically from September 1, 2021 to the current date once the current date is past September 1, 2021.

Thank you once again.

Maybe something like this will work for you.

Weight <- c("100","97","98","100","110","105","100","101","99","110")
Dates <-  c("2019-04-20","2019-05-10","2019-08-10","2020-09-01","2020-09-20","2020-10-20","2021-01-07","2021-01-20","2021-03-10", "2021-06-02")

df  <- data.frame(Dates, Weight)
df$Dates <- as.Date(df$Dates)

CurrYear <- year(Sys.Date())
CurrMonth <- month(Sys.Date())
if(CurrMonth < 9) CurrYear <- CurrYear - 1
df <- filter(df, Dates >= make_date(year = CurrYear, month = CurrMonth, day = 1))
1 Like

Thank you very much! This works perfectly! I appreciate your help and expertise

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.