Home > OS >  Filter by a date condition with an unspecified year in R
Filter by a date condition with an unspecified year in R

Time:04-09

I have a data frame that has a "date" column. The format is: "Y%-m%-d%"

> class(df$date)
[1] "character"

> head(df$date)
[1] "1989-03-01" "2002-03-24" "2021-04-02" "2004-02-01" "2020-03-26" "1996-04-06"

I would like to subset my dataframe, taking out all the dates between [01-01;01-05] (first to fifth january included) no matter the year, which is what is causing me trouble.

How can I do that? Thank you very much.

EDIT: it appears that I might have expressed myself badly. I would like to erase from the dataset the period from the 1st to the 5th of January included. So to keep what comes after, the rest of the year, for each year.

CodePudding user response:

Here is an alternative, converting to character.

df[format(as.Date(df$date), "%m%d") > "0105",]

CodePudding user response:

Here is one option where we take the substring excluding the year part and create a logical vector with 01-01 to 01-05 using %in%

subset(df, !substring(date, 6) %in% sprintf("01-d", 1:5))

Or may convert to Date class and extract the month, day to create the logic

library(dplyr)
library(lubridate)
df %>%
   mutate(date = ymd(date)) %>%
   filter(month(date) == 1, day(date) > 5)
  • Related