将最近的日期返回到R中的给定日期

时间:2017-07-13 13:09:04

标签: r date closest which

我的数据框由个体动物的个体观察组成。每只动物都有一个生日,我想将其与日期向量中最接近的田间季节相关联。

这是一个非常基本的可重复的例子:

ID <- c("a", "b", "c", "d", "a") # individual "a" is measured twice here
birthdate <- as.Date(c("2012-06-12", "2014-06-14", "2015-11-11", "2016-09-30", "2012-06-12"))    
df <- data.frame(ID, birthdate)

# This is the date vector
season_enddates <- as.Date(c("2011-11-10", "2012-11-28", "2013-11-29", "2014-11-26", "2015-11-16", "2016-11-22", "2012-06-21", "2013-06-23", "2014-06-25", "2015-06-08", "2016-06-14"))

通过以下代码,我可以了解生日和最近季节的结束日期。

for(i in 1:length(df$birthdate)){
  df$birthseason[i] <- which(abs(season_enddates-df$birthdate[i]) == min(abs(season_enddates-df$birthdate[i])))
}

然而,我想要的是实际日期,而不是差异。例如,birthseason的第一个值应该是2012-06-21。

5 个答案:

答案 0 :(得分:2)

由于您使用的是您未在示例中包含的变量,因此有点令人困惑。

但我认为这就是你想要的:

for (ii in 1:nrow(df))  df$birthseason[ii] <-as.character(season_enddates[which.min(abs(df$birthdate[ii] - season_enddates))])

或者使用lapply

df$birthseason <- unlist(lapply(df$birthdate,function(x) as.character(season_enddates[which.min(abs(x - season_enddates))])))

结果:

> df
  ID  birthdate birthseason
1  a 2012-06-12  2012-06-21
2  b 2014-06-14  2014-06-25
3  c 2015-11-11  2015-11-16
4  d 2016-09-30  2016-11-22
5  a 2012-06-12  2012-06-21

答案 1 :(得分:2)

您正在寻找哪个season_enddate最接近birthdate[1]birthdate[2]等。

为了获得数据,我将创建一个实际可重现的例子:

birthdate <- as.Date(c("2012-06-12", "2014-06-14", 
                       "2015-11-11", "2016-09-30", 
                       "2012-06-12"))

season_enddates <- as.Date(c("2011-11-10", "2012-11-28", 
                             "2013-11-29", "2014-11-26",
                             "2015-11-16", "2016-11-22", 
                             "2012-06-21", "2013-06-23", 
                             "2014-06-25", "2015-06-08", 
                             "2016-06-14"))

基本上我使用你也使用的功能,除了我决定将它分解一下,所以更容易理解你想要做的事情:

new.vector <- rep(0, length(birthdate))
for(i in 1:length(birthdate)){
    diffs <- abs(birthdate[i] - season_enddates)
    inds  <- which.min(diffs)
    new.vector[i] <- season_enddates[inds]
}

# new.vector now contains some dates that have been converted to numbers:
as.Date(new.vector, origin = "1970-01-01")
# [1] "2012-06-21" "2014-06-25" "2015-11-16" "2016-11-22"
# [5] "2012-06-21"

答案 2 :(得分:1)

我建议对您的问题进行一些编辑,以便您的示例代码生成重现问题所需的所有变量。请查看并检查我是否了解您的问题。

要解决此问题,我建议您使用which.min(让您的代码更简单,更快捷),并结合您的season_enddates向量的子集,如下所示:

for(i in 1:length(younger$HatchCalendarYear)){
  df$birthseasonDate[i] <- season_enddates[which.min(abs(season_enddates - df$birthdate[i]))]
}

答案 3 :(得分:1)

这里的所有解决方案基本相同。如果您希望有一个优化的功能为您执行此操作,我就是这样做的:

match_season <- function(x,y){
  nx <- length(x)
  ind <- numeric(nx)
  for(i in seq_len(nx)){
    ind[i] <- which.min(abs(x[i] - y))
  }
  y[ind]
}

然后你就可以做到:

younger$birthseason <- match_season(younger$HatchDate, season_enddates)

看起来更干净,并以正确的Date格式为您提供所需的输出。

基准:

start <- as.Date("1990-07-01")
end <- as.Date("2017-06-30")

birthdate <- sample(seq(start, end, by = "1 day"), 1000)

season_enddates <- seq(as.Date("1990-12-21"),
                       as.Date("2017-6-21"),
                       by = "3 months")

library(rbenchmark)

benchmark(match_season(birthdate, season_enddates),
          columns = c("test","elapsed"))

给出100次重复的7.62秒的时间。

答案 4 :(得分:1)

在这种情况下,

findInterval很有用。找到每个season_enddates最近的df$birthdate

vec = sort(season_enddates)
int = findInterval(df$birthdate, vec, all.inside = TRUE)
int
#[1]  1  5  8 10  1

我们比较间隔的每个周围日期的距离并选择最小值:

ans = vec[int]
i = abs(df$birthdate - vec[int]) > abs(df$birthdate - vec[int + 1])
ans[i] = vec[int[i] + 1]
ans
#[1] "2012-06-21" "2014-06-25" "2015-11-16" "2016-11-22" "2012-06-21"