Error in writing if else statement

Along the same line as @nirgrahamuk, ifelse seems more appropriate, especially because you can ignore else if completely. If you are actually passing Meditation as a vector (as opposed to scalar) to the function, something like this is likely to do the job:

ifelse(Mediation %in% c("Medication Prior to Start of Visit", "Medication after start of Visit"), End_Date - Start_Date, -1)

The advantage of %in% is that it will not return NA even if Meditation contains NA, and will return FALSE in that case.

For your future posts, consider providing a reproducible example of your problem, regarding which there is a detailed guide here. And, welcome to the community! :slight_smile: