Using lubridate to create factors based on date

Viewed 178

I've tried to find the appropriate answer but all present much simpler cases than what I have. I need to create a 4-level (nov, end_feb, end_apr, other) factor based on the date information in a data frame i have and then add it as a column. Moreover, i need the code to go fast since the real df I have is over 800 thousand rows

Here is what I have so far with lubridate and %within%. It does work but is terribly slow due to inefficincy, since I have to resort to creating a new column with sapply(df, sub_period_gen(date)). Optimally, I need a way to ensure that the solution is vectorized since I have some other factor generators that work on the same data frame and are also slow

sub_period_gen <- function(x){
  i_1 <- ymd("2019-11-01")%--% ymd("2019-11-30")
  i_2 <- ymd("2020-02-24")%--% ymd("2020-02-29")
  i_3 <- ymd("2020-04-24")%--% ymd("2020-04-30")
  if (x %within% i_1){
    return("nov")  # return case one
  } else if (x %within% i_2){
    return("end_feb")  # return case two
  } else if (x %within% i_3){
    return("end_apr")  # return case three
  } else{
    return("other")  # return case four
  }
}

Thanks in advance!

EDIT: I somewhat optimized the solution, but it still looks suboptimal and very hard to modify. Also, i moved intervals into global environment

sub_period_gen <- function(x){
  return(ifelse(x %within% i_1,"nov",ifelse(x %within% i_2,"end_feb",ifelse(x %within% i_3,"end_apr","other"))))
  }

My question differs from this one since there is really no regularity in my date and the breaks are for the particular analysis.

EDIT 2: sample input:

library(lubridate)
toy <- tibble(date = ymd("2019-11-12","2020-03-11","2020-01-31","2019-12-19","2019-12-04","2020-01-21","2020-01-31","2020-02-16",
              "2020-02-28","2020-03-20","2020-02-08","2020-03-23","2020-01-22","2020-02-18","2020-03-19","2019-11-22",
              "2020-01-14","2020-03-04","2019-12-02","2019-11-03","2020-02-27","2020-02-13","2019-11-17","2020-03-17",
              "2020-04-14","2019-12-19","2019-11-05","2020-01-11","2020-04-25","2019-11-24"))

desired output:

>  date         sub_period
>   <date>     <chr>     
> 1 2019-11-12 nov       
> 2 2020-03-11 other
> 3 2020-01-31 other   
> 4 2019-12-19 other   
> 5 2019-12-04 other   
> 6 2020-01-21 other   
> 7 2020-02-29 end_feb   
> 8 2020-02-16 other   
> 9 2020-04-28 end_apr 
2 Answers

Here's an approach with case_when from dplyr:

library(dplyr)
library(lubridate)
toy %>%
  mutate(sub_period = 
         case_when(date >= ymd("2019-11-01") & date < ymd("2019-11-30") ~ "nov",
                   date >= ymd("2020-02-24") & date < ymd("2020-02-29") ~ "end_feb",
                   date >= ymd("2020-04-24") & date < ymd("2020-04-30") ~ "end_apr",
                   TRUE ~ "other"))
# A tibble: 30 x 2
   date       sub_period
   <date>     <chr>     
 1 2019-11-12 nov       
 2 2020-03-11 other     
 3 2020-01-31 other     
 4 2019-12-19 other     
 5 2019-12-04 other     
 6 2020-01-21 other     
 7 2020-01-31 other     
 8 2020-02-16 other     
 9 2020-02-28 end_feb   
10 2020-03-20 other     
# … with 20 more rows

If you need substantially more speed, you could do a non-equi join with data.table's IDate class. First you need to set up a separate table to join onto:

library(data.table)
setDT(toy)
toy[,date:=as.IDate(date)]

date.table <- data.table(Start = c(as.IDate("2019-11-01"),as.IDate("2020-02-24"),as.IDate("2020-04-24")),
                         End = c(as.IDate("2019-11-30"),as.IDate("2020-02-29"),as.IDate("2020-04-30")),
                         sub_period = c("nov","end_feb","end_apr"))

date.table
        Start        End sub_period
1: 2019-11-01 2019-11-30        nov
2: 2020-02-24 2020-02-29    end_feb
3: 2020-04-24 2020-04-30    end_apr

And then perform the join:

date.table[toy, on = .(Start<=date, End>date)][is.na(sub_period),sub_period := "other"][]
         Start        End sub_period
 1: 2019-11-12 2019-11-12        nov
 2: 2020-03-11 2020-03-11      other
 3: 2020-01-31 2020-01-31      other
 4: 2019-12-19 2019-12-19      other
 5: 2019-12-04 2019-12-04      other
 6: 2020-01-21 2020-01-21      other
 7: 2020-01-31 2020-01-31      other
 8: 2020-02-16 2020-02-16      other
 9: 2020-02-28 2020-02-28    end_feb
10: 2020-03-20 2020-03-20      other
...

In base R you can use a nested ifelse function like this:

sub_period_gen <- function(x){
ifelse(x >= ymd("2019-11-01") & x <= ymd("2019-11-30"), "nov",
ifelse(x >= ymd("2020-02-24") & x <= ymd("2020-02-29"), "end_feb",
ifelse(x >= ymd("2020-04-24") & x <= ymd("2020-04-30"), "end_apr",
"other")))
}

To get the desired output you can bind the input and the output like this cbind.data.frame(toy,sub_period= sub_period_gen(toy$date)).

Related