1

I need to create a new variable (Var_new) out of the values of two old variables (Var_Old_1; Var_Old_2) but in both cases only for a subset of rows.

Let me explain:

  1. I have an index variable, indicating the patient code; called "patientcode" (H01, H02, H03 ...)

  2. I need to create a new variable. "Var_new"

  3. the values of the new Variable "Var_new" must be assigned from Var_Old_1 or Var_Old_2 depending on the index variable.

So e.g.

for (H01, H02, H04) Var_new == Var_Old_1 for (H03, H05) Var_new == Var_Old_2

patientcode Var_Old_1 Var_Old_2 Var_new
H01 1.1 2.1 1.1
H02 1.2 2.2 1.2
H03 1.3 2.3 2.3
H04 1.4 2.4 1.4
H05 1.5 2.5 2.5

This is only an example. In my dataset I'am dealing with 300 rows and I need to create 32 new variables following this scheme. so a good code would be helpful.

Verena
  • 21
  • 1

1 Answers1

0

Maybe something like the following?

patientcode_old1 <- c("H01", "H02", "H04")
patientcode_old2 <- c("H03", "H05")

result <- df %>%
  mutate(Var_new = case_when(
          patientcode %in% patientcode_old1 ~ Var_Old_1,
          patientcode %in% patientcode_old2 ~ Var_Old_2,
          TRUE ~ NA_real_
          )
        )

Yet your description sounds like you need to do lots of manual prework if you need to create 32 new variables that way?! Maybe think about if there's any kind of regularity behind the way those vars need to be created. Otherwise I don't see a possibility how to avoid further manual efforts.

TomS
  • 226
  • 3
  • 10