0

I have a list of patients who have pain, function, and well-being scores before and after surgery. I want to use a criteria to decide if each patient has improved or not improved after surgery.

The criteria is complex, and I want to use the dplyr package and the case_when function to add an additional column - Improved - with outcomes yes or no based on the criteria.

The criteria is as follows:

The patient is considered improved if:

  1. The pain or function score has changed by >50% AND the absolute value for either score has changed by >20.

OR

The patient is considered improved if 2/3 criteria are met:

  1. The pain score changes by >20% AND the absolute value has changed by >10.
  2. The function score changes by >20% AND the absolute value has changed by >10.
  3. The wellbeing score changes by >20% AND the absolute value has changed by >10.
user438383
  • 5,716
  • 8
  • 28
  • 43
  • 2
    It will help to make some sample data. E.g. `data.frame(patient = 1:3, pain_pre = c(30, 50, 60), pain_post = c(15, 25, 65))` – Dan Adams Jan 20 '22 at 01:48
  • 4
    Without a reproducible example, it is difficult to provide specific solutions. I feel `case_when` may not be the best approach. – Zhiqiang Wang Jan 20 '22 at 01:57
  • 2
    Welcome to SO, Yushy Zhou! You speak of variables like `pain changed` and `absolute value has changed`, but those can be ambiguous. This is a simple application of compound logical statements, almost certainly, but there is too much uncertainty here to even begin to give you working code. Please see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jan 20 '22 at 02:14

1 Answers1

0

Without code, it'd hard to tell, but something like this should do the trick. It's just a matter of getting the right combination of conditions.

df_new = df %>% mutate(improvement = case_when(pain_score_change > 0.2 & function_score_change >0.2 & absolute_value >= 0.1  ~ "improved",
                                           function_score_change > 0.2 & wellbeing_score_change > 0.2 & absolute_value >= 0.1 ~ "improved",
                                           wellbeing_score_change > 0.2 & pain_score_change > 0.2 & absolute_value >= 0.1 ~ "improved",
                                           pain_score_change > 0.5 & wellbeing_change > 0.2 ~ "improved",
                                           pain_score_change > 0.2 & wellbeing_change > 0.5 ~ "improved")
thehand0
  • 1,123
  • 4
  • 14