How to get to the next Tuesday before April 1st every year?

I have a question about Date. I need to define a date which is the nearest Tuesday to April 1st, every year from 1961 to 2006.

How can I check from the Calendar I see the following:

1961    1961-04-04
1962    1962-04-03
1963    1963-04-02
1964    1964-03-31

      

But instead of looking at them from the Calendar, can I do it easily in R?


This issue has experience in air pollution network management. Specifically, the UK's historic smoke and sulfur dioxide network, operating between 1961 and 2005, has been governed on a "pollution year" and such stars of the year from the date specified in this question.

+3


source to share


1 answer


Here is one solution



nearestTuesday <- function(date) {
  delta <- as.POSIXlt(date)$wday - 2
  if(abs(delta) < 4) 
    next_date <- as.Date(date) - delta
  else 
    next_date <- as.Date(date) + (7 - delta)
  return(next_date)
}

lapply(ISOdate(1961:2006, 4,1), nearestTuesday)

      

+5


source







All Articles