Improvement to this nested code?

Hi, out of curiosity I was wondering if this nested ifelse() statement I have used could be made more efficient? I am aware I could create a function out of this but I am unsure where to start with it.

I'm attaching the string of code I have written but avoiding the df as there is quite a lot of data to work with. Hoping it would be alright to just attach this nested statement and see if anyone would have an idea on how to make it more concise.

Home.raw5 <- Home.raw5 %>%
  mutate(VISIT_DAY = if_else((PATIENT_ID == "-2147483646" & DATE %in% dmy(c("06-08-2018", "20-08-2018", "01-10-2018", "04-02-2019","22-07-2019"))), 
                             "YES",
                             if_else((PATIENT_ID == "-2147483645" & DATE %in% dmy(c("16-08-2018", "30-08-2018", "11-10-2018", "27-02-2019"))), 
                                     "YES", 
                                     if_else((PATIENT_ID == "-2147483644" & DATE %in% dmy(c("06-09-2018", "21-09-2018", "02-11-2018", "01-03-2019", "05-09-2019"))), 
                                             "YES", 
                                             if_else((PATIENT_ID == "-2147483643" & DATE %in% dmy(c("21-09-2018", "02-11-2018", "20-03-2019", "06-09-2019"))), 
                                                     "YES", 
                                                     if_else((PATIENT_ID == "-2147483642" & DATE %in% dmy(c("27-09-2018", "12-10-2018", "22-11-2018", "28-03-2019", "26-09-2019"))), 
                                                             "YES",
                                                             if_else((PATIENT_ID == "-2147483641" & DATE %in% dmy(c("25-10-2018", "08-11-2018", "04-12-2018", "18-12-2019", "18-04-2019"))), 
                                                                     "YES", 
                                                                     if_else((PATIENT_ID == "-2147483640" & DATE %in% dmy(c("06-11-2018", "22-11-2018", "04-01-2018", "23-05-2019"))), 
                                                                             "YES", 
                                                                             if_else((PATIENT_ID == "-2147483639" & DATE %in% dmy(c("23-11-2018", "03-01-2019", "07-05-2019"))), 
                                                                                     "YES", 
                                                                                     if_else((PATIENT_ID == "-2147483638" & DATE %in% dmy(c("17-01-2019", "31-01-2019", "14-03-2019", "04-07-2019"))), 
                                                                                             "YES", 
                                                                                             if_else((PATIENT_ID == "-2147483637" & DATE %in% dmy(c("25-02-2019", "11-03-2019", "15-04-2019", "19-09-2019"))), 
                                                                                                     "YES",
                                                                                                     if_else((PATIENT_ID == "-2147483635" & DATE %in% dmy(c("12-03-2019", "26-03-2019", "14-05-2019", "24-09-2019"))), 
                                                                                                             "YES", 
                                                                                                             if_else((PATIENT_ID == "-2147483633" & DATE %in% dmy(c("25-04-2019", "08-05-2019", "20-06-2019"))), 
                                                                                                                     "YES",
                                                                                                                     if_else((PATIENT_ID == "-2147483632" & DATE %in% dmy(c("20-05-2019", "15-07-2019"))), 
                                                                                                                             "YES",
                                                                                                                             if_else((PATIENT_ID == "-2147483631" & DATE %in% dmy(c("29-05-2019", "12-06-2019", "24-07-2019"))), 
                                                                                                                                     "YES",
                                                                                                                                     if_else((PATIENT_ID == "-2147483630" & DATE %in% dmy(c("17-09-2019", "01-10-2019"))), 
                                                                                                                                             "YES", "NO"))))))))))))))))

Created on 2020-02-03 by the reprex package (v0.3.0)

Thanks for any help!

Make a table to hold your conditions and then join your data to it

1 Like

You can also use case_when, but suggestion from @nirgrahamuk is also worth considering.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.