找到最接近日期的月份日,无论年份

时间:2014-09-15 09:24:53

标签: r date

我有以下日期向量(长度= 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")

日期数据框,包含125行和51863列。对于向量的每个日期,我的数据框中有125个相应的日期。

我将举例说明一个日期:

对于日期“1871-01-01”,我有相应的日期:

[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"

在这125个日期中,我想找到最接近“1871-01-01”的20个日期,无论年份是什么。所以实际上我只是想找到“月 - 日”最接近“01-01”的20个日期。

为此,我将日期改为“今年的一天”,所以我的125个相应日期变为:

[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"

我可以看看这个向量和数字01之间的区别(对于01-01)。问题是,对于第12-31天,我得到数字“365”,当我做差异时,我有“364”,而“12-31”是最接近“01-01”的日期之一。 / p>

我怎样才能找到最接近“01-01”的20个日期,并考虑到12月是最接近1月份的月份之一,而7月份是更进一步的日期?

1 个答案:

答案 0 :(得分:1)

假设x是您的“目标日期”,y是您想要找到最接近x的日期的向量,您的定义为“关闭” ”。我建议这样的事情:

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

当然,这不会考虑到闰年。