Search code examples
rdateif-statementmutate

Mutate/ifelse conditions with dates to return a numeric object


In R: Trying to create a new column that indicates numerically whether another column containing dates has met time period criteria. The code runs, but it doesn't return correct values (ie, each entry is returned in the new column as 1, when only half of those entries met the "1" condition.

DF %>% 
  mutate(g = ifelse(Program.Start.Date >= 2023-08-03 | Program.Start.Date <= 2023-11-30, 1, 
                    ifelse(Program.Start.Date <= 2023-08-03 | Program.Start.Date>= 2023-11-30, 0)))

I also tried:

DF %>% 
  mutate(g = ifelse(Program.Start.Date >= 2023-08-03 | Program.Start.Date <= 2023-11-30, 1, 0))

All rows in the new column G return "1". I also converted the column "Program.Start.Date" to Year Month Date format using Lubridate.


Solution

  • Two things are at issue:

    1. You are not quoting your date values inside your ifelse() statement, so R is treating them as an arithmetic equation
    2. | means 'or' in R, I think you mean &. In your code, all dates will equal TRUE. Try this:
    library(lubridate)
    library(dplyr)
    
    # Sample data
    df <- data.frame(Program.Start.Date = seq(ymd("2023-08-01"), 
                                              ymd("2023-11-30"), 
                                              by = "day"))
    
    df <- df %>% 
      mutate(g = ifelse(Program.Start.Date >= "2023-08-03" & 
                          Program.Start.Date <= "2023-11-30", 1, 0))
    
    head(df)
      Program.Start.Date g
    1         2023-08-01 0
    2         2023-08-02 0
    3         2023-08-03 1
    4         2023-08-04 1
    5         2023-08-05 1
    6         2023-08-06 1