I have a dataset with patients undergoing radical prostatectomy.
Variables starting with ANM_1_surgery identify patients who had surgery before prostatectomy, and their value is 0/1
I want to create a new variable called prev_surg, with values 0/1.
If a variable starting with ANM_1_surgery has a value of 1, the prev_surg must be 1.
I would create prev_surg variable this way:
df_clean <- df_raw %>%
mutate(
prev_surg =
case_when(
ANM_1_surgery_tonsillectomy == 1 ~ 1,
ANM_1_surgery_appendicectomy == 1 ~ 1,
ANM_1_surgery_colectomy == 1 ~ 1,
# and so on, for all the other variables starting with ANM_1_surgery
TRUE ~ 0,
)
)
)
I am looking for a quicker code, that helps me select all variables starting with ANM_1_surgery, without typing each one into the case_when code. Something like
df_clean <- df_raw %>%
mutate(
prev_surg =
case_when(
(all the variables starting with ANM_1_surgery) == 1 ~ 1,
TRUE ~ 0,
)
)
)