jeudi 17 septembre 2020

Optimizing If Else for Date variables in R

I am working with a dataset with information about cities with Covid and I would like to add a variable if city x in date t was in quaratine, I created an ifelse code but it takes a lot of time for the 2.3MM observations I have.

My dataset looks like this

City     Date
a      2020-03-04    
b      2020-03-04
a      2020-03-05    
b      2020-03-05
a      2020-03-06   
b      2020-03-06 
a      2020-03-07 
b      2020-03-07 

And my code is this

library(data.table
df <- df %>% 
   mutate(quarantine = 0)
df <- df %>%
   mutate(quarantine = fifelse(City = "a",
                              fifelse(Date > as.Date("2020-03-05") &
                                      Date <= as.Date("2020-03-06"), 1, 0),
                              quarantine) %>%
   mutate(quarantine = fifelse(City = "b",
                              fifelse(Date > as.Date("2020-03-06") &
                                      Date <= as.Date("2020-03-07"), 1, 0),
                              quarantine) 

And that repeated until I complete the 34 cities I have, it is a really long code but I don't know how to optimize it because every city has a different date for quarantine so I don't know if I can loop it.

Thanks for the help

Aucun commentaire:

Enregistrer un commentaire