Home > Net >  Filter on date in r dataframe
Filter on date in r dataframe

Time:11-23

I am attempting to limit my dataframe to the days of each month between the 20th and the 25th . I got a big dataset with many dates ranging over many years. It looks something like this:

Event          Date
Football       20.12.2016
Work           15.10.2019
Holiday        30.11.2018
Running        24.01.2020

I would then like to restrict my results to:

Event          Date
Football       20.12.2016
Running        24.01.2020

Any tips on how to do this?

CodePudding user response:

This is a solution using dplyr/lubridate although I have converted your Date column using as.Date

 df <-
      data.frame(
        Event = c("Football", "Work", "Holiday", "Running"),
        Date = c("20.12.2016", "15.10.2019", "30.11.2018", "24.01.2020")
      )

df$Date <- as.Date(df$Date, format = "%d.%m.%Y")

df %>% filter(day(Date) >= 20 & day(Date) <= 25)

Output

1 Football 2016-12-20
2  Running 2020-01-24

CodePudding user response:

Doing a literal string-match, keeping your Date column as strings (not real dates):

# base R
subset(quux, between(as.integer(sub("\\..*", "", Date)), 20, 25))
#      Event       Date
# 1 Football 20.12.2016
# 4  Running 24.01.2020

# dplyr
quux %>% 
  filter(between(as.integer(sub("\\..*", "", Date)), 20, 25))

between can be from dplyr::, data.table::, or we can easily craft our own with:

between <- function(x, y, z) y <= x & x <= z

... though the two package versions are more robust to NAs and other issues.


Data

quux <- structure(list(Event = c("Football", "Work", "Holiday", "Running"), Date = c("20.12.2016", "15.10.2019", "30.11.2018", "24.01.2020")), class = "data.frame", row.names = c(NA, -4L))
  • Related