R ifelse vs if else vs for loop - datediff excluding weekends

61 Views Asked by At

I am trying to calculate whether or not a person canceled their appointment within the proper timeframe. I am confident there is a cleaner way to do this, and I am open to suggestions, but this is where I am now.

I have calculated the day of the week of the appointment and I have calculated the difference between that appointment date and the cancel date. If appt is on a Monday and I want to subtract 2 from the corresponding datediff value. Otherwise do nothing.

I tried doing it using ifelse but I can't seem to figure out how to make that compute something when the result is TRUE, only display values.

I am thinking something along the lines of

If (df7$weekday =='Monday') {
df7$cancel_lead_time - 2
}

P.S. For anyone wondering I am going to tackle holidays after I solve this

1

There are 1 best solutions below

2
On BEST ANSWER

I don't think I've understood properly; is this what you're trying to do?

if(df7$weekday == 'Monday') {
  df7$cancel_lead_time - 2
} else {
  df7$cancel_lead_time
}