Search code examples
rdplyrlubridate

How to remove all rows in R that have a specific month?


I have a data.frame where in one column I have a lot of different dates in the format Year-Month-Day and I would like to keep only the rows that have as a month 12, so December.

I tried two different codes:

First version:

IBES1985_1990[IBES1985_1990$`Forecast Period End Date, SAS Format` != 
                month(1,2,3,4,5,6,7,8,9,10,11, )] 

But here I get an error saying that undefined columns where selected.

Second version:

IBES1985_1990 <- IBES1985_1990 %>%
  mutate(`Forecast Period End Date, SAS Format`= ifelse(month(`Forecast Period End Date, SAS Format`)
         %in% c(1,2,3,4,5,6,7,8,9,10,11),NA,`Forecast Period End Date, SAS Format`))

Here I wanted to then delete all the rows that have NA in it but the date format changed to pure numbers and I couldn't change it back to see if I the dates that don't have December were already deleted or not.

In summary, I would like to have a code where all rows are deleted that are not December.


Solution

  • If your data looks like this

    library(lubridate)
    
    df <- data.frame(dates = seq.Date(ymd("2022-09-02"), ymd("2023-02-02"), "month"), 
      data = 1:6)
    df
           dates data
    1 2022-09-02    1
    2 2022-10-02    2
    3 2022-11-02    3
    4 2022-12-02    4
    5 2023-01-02    5
    6 2023-02-02    6
    

    keep all December dates e.g. by using strftime

    df[strftime(df$dates, format="%b") == "Dec", ]
           dates data
    4 2022-12-02    4
    

    With dplyr you can do

    library(dplyr)
    
    df %>% 
      rowwise() %>% 
      summarize(dates = dates[strftime(dates, format="%b") == "Dec"], data)
    # A tibble: 1 × 2
      dates       data
      <date>     <int>
    1 2022-12-02     4
    

    or, if you want to use lubridates month

    library(dplyr)
    library(lubridate)
    
    df %>% 
      rowwise() %>% 
      summarize(dates = dates[month(dates) == 12], data)
    # A tibble: 1 × 2
      dates       data
      <date>     <int>
    1 2022-12-02     4