Chika
Chika

Reputation: 1497

Find the closest month-day to a date, no matter the year

I have the following vector of dates (length = 51863)

dates_sim <- seq(from=as.Date("1871-01-01", format = "%Y%m%d"), to=as.Date("2012-12-31", format = "%Y%m%d"), by="days")

And a data frame of dates, with 125 rows and 51863 columns. For each date of my vector, I have 125 corresponding dates in my data frame.

I'll take an example with only one date:

For the date "1871-01-01" I have these corresponding dates:

[1] "2007-12-23" "1964-01-01" "1995-01-03" "1997-02-01" "1993-03-28" "1978-01-21" "1986-12-10" "1982-02-20" "1993-11-16" "1973-12-10" "1964-11-30" "1997-10-29"
[13] "1999-03-15" "1993-01-02" "1993-02-25" "1985-02-19" "2006-03-13" "1976-03-10" "1976-11-15" "1986-03-11" "1975-02-23" "1962-01-31" "1959-11-08" "1979-02-18"
[25] "2003-02-08" "1983-12-13" "2006-03-13" "1984-02-16" "1997-02-01" "1976-03-20" "2000-01-12" "1976-11-15" "1968-12-12" "1979-02-18" "1976-03-10" "1975-01-02"
[37] "1998-11-23" "1979-01-15" "1991-01-28" "1986-12-10" "1959-11-08" "1966-01-06" "1970-12-16" "1995-01-05" "1959-03-13" "1966-03-05" "1971-03-01" "1975-02-22"
[49] "1962-01-30" "1958-11-12" "1964-01-01" "2000-01-10" "1982-02-24" "2003-02-11" "1993-02-24" "1962-01-30" "1976-03-20" "1989-01-20" "1975-01-02" "2002-12-03"
[61] "2003-02-08" "1993-01-01" "1973-12-10" "1998-12-08" "1993-03-28" "1994-12-24" "1983-12-13" "1964-01-18" "1987-01-18" "1959-02-09" "1998-11-24" "1983-12-07"
[73] "2006-03-13" "1972-12-20" "2007-12-23" "1984-02-17" "1959-03-13" "1964-01-01" "1985-03-08" "1973-01-01" "2003-12-31" "1994-12-24" "1972-01-18" "1995-01-14"
[85] "1998-12-07" "1966-03-05" "2007-11-17" "1983-12-07" "1986-03-13" "1964-01-18" "1972-02-28" "1992-02-26" "1992-12-31" "1976-03-10" "1974-11-06" "1995-01-05"
[97] "2003-02-08" "1979-02-18" "1969-12-19" "1969-12-13" "1959-02-09" "1976-03-20" "1986-03-11" "1998-11-22" "1982-02-20" "1969-12-20" "1966-01-09" "1979-02-19"
[109] "1981-02-25" "1984-02-18" "1982-02-19" "1966-01-05" "1987-01-18" "1991-11-24" "1998-01-21" "1983-03-02" "1979-02-18" "1984-01-10" "1984-02-17" "1962-01-31"
[121] "2002-11-05" "1974-03-06" "1971-03-01" "2002-01-14" "2000-01-11"

Among these 125 dates, I want to find the 20 dates which are the closest to "1871-01-01" no matter what the year is. So in fact I just want to find the 20 dates where "month-day" is the closest to "01-01".

For that, I transformed the dates to "day oh the year" so my 125 corresponding dates became:

[1] "357" "001" "003" "032" "087" "021" "344" "051" "320" "344" "335" "302" "074" "002" "056" "050" "072" "070" "320" "070" "054" "031" "312" "049" "039" "347"
[27] "072" "047" "032" "080" "012" "320" "347" "049" "070" "002" "327" "015" "028" "344" "312" "006" "350" "005" "072" "064" "060" "053" "030" "316" "001" "010"
[53] "055" "042" "055" "030" "080" "020" "002" "337" "039" "001" "344" "342" "087" "358" "347" "018" "018" "040" "328" "341" "072" "355" "357" "048" "072" "001"
[79] "067" "001" "365" "358" "018" "014" "341" "064" "321" "341" "072" "018" "059" "057" "366" "070" "310" "005" "039" "049" "353" "347" "040" "080" "070" "326"
[105] "051" "354" "009" "050" "056" "049" "050" "005" "018" "328" "021" "061" "049" "010" "048" "031" "309" "065" "060" "014" "011"

And I can look at the difference between this vector and number 01 (for 01-01). The problem is that, for the day 12-31 I get the number "365" and when I do the difference I have "364" whereas the "12-31" is one of the closest dates to "01-01".

How can I do to look for the 20 closest dates to "01-01", and taking into account that december is one of the closest month to january and july is the further?

Upvotes: 0

Views: 236

Answers (1)

nicola
nicola

Reputation: 24510

Let's say that x is your "target date" and y is your vector of dates from which you want to find the closest one to x, with your definition of "close". I'd suggest something like this:

daysDiff<-abs(as.POSIXlt(x)$yday-as.POSIXlt(y)$yday)
which.min(pmin(daysDiff,365-daysDiff))

Of course this will not take into account leap years.

Upvotes: 1

Related Questions