I have a dataset with the English and Spanish version of a questionnaire. The questionnaires ask whether individuals have ever received a large number of different diagnoses. Each variable takes the form prev_dx_major_depression
for the English data and prev_dx_major_depression_span
for the Spanish data.
I would like to combine the two into a single variable. I am currently using the following code to achieve this purpose:
mutate(
prev_dx_major_depression = if_else(prev_dx_major_depression == 1 |
prev_dx_major_depression_span == 1,
1, 0
))
However, I know this is highly inefficient for such a large number of variables. My hunch is that I'll need to use some combination of mutate_at
, recode
, starts_with
and ends_with
. However, I am a bit stuck at this point and am not sure how to match up the corresponding variables together.
Here is some sample data:
sample_data <-
structure(
list(
id = 1:5,
prev_dx_major_depression = c(0, 1, 1,
0, 0),
prev_dx_bipolar = c(0, 0, 0, 0, 0),
prev_dx_generalized_anxiety = c(1,
1, 0, 0, 0),
prev_dx_major_depression_span = c(NA, NA, NA, NA,
1),
prev_dx_bipolar_span = c(NA, NA, NA, NA, NA),
prev_dx_generalized_anxiety_span = c(NA,
NA, NA, NA, 1)
),
class = "data.frame",
row.names = c(NA,-5L)
)