Home > Software design >  In R, how can I identify which date in a column is closest to a pre-specified date?
In R, how can I identify which date in a column is closest to a pre-specified date?

Time:04-04

I have a dataset and I'm trying to find the closet date to '2021-08-02' without going before it. So, in my sample dataset below, I want it to filter out everything before '2021-08-05' since that is the nearest date without going sooner.

library(dplyr)
test <- tibble(Day = seq(as.Date("2021-08-01"), as.Date("2021-09-10"), by="4 days"),
                Score = c(sample(1:15, 11)))

CodePudding user response:

Get the absolute difference between them, find the minimum index, and return the sequence

library(dplyr)
date <- as.Date('2021-08-02')
test %>% 
   slice(seq_len(which.min(abs(date - Day))))

-output

# A tibble: 1 × 2
  Day        Score
  <date>     <int>
1 2021-08-01    11

If we want to return the other set of rows, either use %>% anti_join(df, .) after the slice step or create a condition with row_number()

test %>% 
   filter(row_number() > seq_len(which.min(abs(date - Day))))
# A tibble: 10 × 2
   Day        Score
   <date>     <int>
 1 2021-08-05     9
 2 2021-08-09    12
 3 2021-08-13    13
 4 2021-08-17     6
 5 2021-08-21     4
 6 2021-08-25     1
 7 2021-08-29    10
 8 2021-09-02     3
 9 2021-09-06     7
10 2021-09-10     8
  • Related