25

I would like to identify the closest date in a vector of given date. Let's say I have the following date vector (with 5 random dates):

coldate= as.Date(c("2013-08-03", "2013-09-04", "2013-09-08", "2013-09-12", "2013-11-01"));

Now, I want to find the closest date to x = as.Date("2013-10-01") inside this vector.

Here is my code :

> which((coldate-x) == min(coldate-x))
  [1] 1

The result should be 4, since the date "2013-09-12" is the closest. But, I have 1... What's wrong in my code?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Henri
  • 1,571
  • 5
  • 23
  • 38

3 Answers3

30

you miss an abs to take care of negative values:

which(abs(coldate-x) == min(abs(coldate - x)))
[1] 4
Arun
  • 116,683
  • 26
  • 284
  • 387
9

See also the which.min function:

R> which.min(abs(x-coldate))
[1] 4
rcs
  • 67,191
  • 22
  • 172
  • 153
  • 1
    While `which.min` seems like the obvious choice, the advantage of the answer by @Arun is that it will return multiple values if there are multiple minimum values. How you deal with those cases might actually be an issue in making a clean dataset, but it is something that should be considered. – thelatemail Feb 28 '13 at 22:08
  • Thanks for your comment @thelatemail. Now I sort coldate before using `which.min` to get the 'lower' match. – Stefan Jelkovich Dec 28 '20 at 21:06
7

The which.closest() function from the birk package is a simple option.

coldate= as.Date(c("2013-08-03", "2013-09-04", "2013-09-08", "2013-09-12", "2013-11-01"))
x = as.Date("2013-10-01")

which.closest(coldate, x)
[1] 4
CephBirk
  • 6,422
  • 5
  • 56
  • 74