Subset dataframe where date is within x days of a vector of dates in R

user1165199

I have a vector of dates e.g.

dates <- c('2013-01-01', '2013-04-02', '2013-06-10', '2013-09-30')

And a dataframe which contains a date column e.g.

df <- data.frame(
                'date' = c('2013-01-04', '2013-01-22', '2013-10-01', '2013-10-10'),
                'a'    = c(1,2,3,4),
                'b'    = c('a', 'b', 'c', 'd')
                )

And I would would like to subset the dataframe so it only contains rows where the date is less than 5 days after any of the dates in the 'dates' vector.

i.e. The initial dataframe looks like this

date       a b 
2013-01-04 1 a
2013-01-22 2 b
2013-10-01 3 c
2013-10-10 4 d

After the query I would only be left with the first and third row (since 2013-01-04 is within 5 days of 2013-01-01 and 2013-10-01 is within 5 days of 2013-09-30)

Does anyone know of the best way to do this?

Thanks in advance

eddi

This is easy (and very fast) to do with a data.table roll:

library(data.table)
dt = data.table(df)

# convert to Date (or IDate) to have numbers instead of strings for dates
# also set the key for dates for the join
dt[, date := as.Date(date)]
dates = data.table(date = as.Date(dates), key = 'date')

# join with a roll of 5 days, throwing out dates that don't match
dates[dt, roll = 5, nomatch = 0]
#         date a b
#1: 2013-01-04 1 a
#2: 2013-10-01 3 c

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Subset dataframe where date is within x days of a vector of dates in R

From Dev

subset date vector by months R

From Dev

R dataframe subset one column to a dataframe (not to a vector)

From Dev

in Twig how to compare if date is within X days?

From Dev

Subset based on a range of dates falling within two date variables

From Dev

Subset dataframe by unique values within a column in R

From Dev

How to choose the subset of a dataframe using a vector in R

From Dev

List dates occurring at intervals of n days within date range

From Dev

R - count how many rows in a data frame have same value and date is within x days

From Dev

Convert days to calendar dates within a data frame in R

From Dev

How to subset a dataframe where a column 'contains' the contents of a vector

From Dev

Finding date where conditions within 30 days has elapsed

From Dev

SQL Server: Return records within X days of a date

From Dev

How to extract date and calculate if it is within "x" days from today?

From Dev

how to take mean of a subset of a dataframe within for loop in r

From Dev

In R, how do you subset rows of a dataframe based on values in a vector

From Dev

get subset dataframe by date

From Dev

R - convert Date column in days since the earliest date in a dataframe

From Dev

R: Subset vector by names

From Dev

How to convert timestamps within a R dataframe to date

From Dev

Creating a dataframe where each cell is a vector in R

From Dev

SQL show closest available date within 30 days from a range of dates

From Dev

Count occurrence of IDs within the last x days in R

From Dev

Fetch all results in table where date is approaching within the next 30 days of current date?

From Dev

R - check if string contains dates within specific date range

From Dev

R - check if string contains dates within specific date range

From Dev

subset dataframe based on conditions in vector

From Dev

MySQL Selecting Dates Within Number of Days

From Dev

MySQL Selecting Dates Within Number of Days

Related Related

  1. 1

    Subset dataframe where date is within x days of a vector of dates in R

  2. 2

    subset date vector by months R

  3. 3

    R dataframe subset one column to a dataframe (not to a vector)

  4. 4

    in Twig how to compare if date is within X days?

  5. 5

    Subset based on a range of dates falling within two date variables

  6. 6

    Subset dataframe by unique values within a column in R

  7. 7

    How to choose the subset of a dataframe using a vector in R

  8. 8

    List dates occurring at intervals of n days within date range

  9. 9

    R - count how many rows in a data frame have same value and date is within x days

  10. 10

    Convert days to calendar dates within a data frame in R

  11. 11

    How to subset a dataframe where a column 'contains' the contents of a vector

  12. 12

    Finding date where conditions within 30 days has elapsed

  13. 13

    SQL Server: Return records within X days of a date

  14. 14

    How to extract date and calculate if it is within "x" days from today?

  15. 15

    how to take mean of a subset of a dataframe within for loop in r

  16. 16

    In R, how do you subset rows of a dataframe based on values in a vector

  17. 17

    get subset dataframe by date

  18. 18

    R - convert Date column in days since the earliest date in a dataframe

  19. 19

    R: Subset vector by names

  20. 20

    How to convert timestamps within a R dataframe to date

  21. 21

    Creating a dataframe where each cell is a vector in R

  22. 22

    SQL show closest available date within 30 days from a range of dates

  23. 23

    Count occurrence of IDs within the last x days in R

  24. 24

    Fetch all results in table where date is approaching within the next 30 days of current date?

  25. 25

    R - check if string contains dates within specific date range

  26. 26

    R - check if string contains dates within specific date range

  27. 27

    subset dataframe based on conditions in vector

  28. 28

    MySQL Selecting Dates Within Number of Days

  29. 29

    MySQL Selecting Dates Within Number of Days

HotTag

Archive