0

I am trying to create a new variable out of two variables in the same dataframe (df), like those below. The categories are mutually exclusive.

VAR1    VAR2
1       1
2       2
6       6

1 = yes
2 = no
6 = did not answer

The script I have tried to get the combined variable, but is not working is below:

if (df$VAR1 == 1) {
  df$combo = 1
} else if (df$VAR2 == 1) {
  df$combo = 2
} else if  ((df$VAR1 == 2) & (df$VAR2 == 2)) {
  df$combo = 3
} else if ((df$VAR1 == 6) & (df$VAR2 == 6)) {
  df$combo = 6
}

Any pointers will be appreciated.

GSA
  • 751
  • 8
  • 12
  • You should be treated with `the condition has length > 1` error / warning when using `if()` with vectors, which in turn leads to previous questions like https://stackoverflow.com/questions/72672134/if-statement-error-the-condition-has-length-1-and-only-the-first-element-wi – margusl Feb 16 '23 at 09:09

1 Answers1

1

You may try

for (i in 1:nrow(df)){
  if (df$VAR1[i] == 1) {
    df$combo[i] = 1
  } else if (df$VAR2[i] == 1) {
    df$combo[i] = 2
  } else if  ((df$VAR1[i] == 2) & (df$VAR2[i] == 2)) {
    df$combo[i] = 3
  } else if ((df$VAR1[i] == 6) & (df$VAR2[i] == 6)) {
    df$combo[i] = 6
  }      
}

  VAR1 VAR2 combo
1    1    1     1
2    2    2     3
3    6    6     6

Or use dplyr

library(dplyr)
df %>%
  mutate(combo = case_when(
    VAR1 == 1 ~ 1,
    VAR2 == 1 ~ 2,
    (VAR1 == 2 & VAR2 == 2) ~ 3,
    (VAR1 == 6 & VAR2 == 6) ~ 6,
    TRUE ~ NA_real_
  ))


  VAR1 VAR2 combo
1    1    1     1
2    2    2     3
3    6    6     6
Park
  • 14,771
  • 6
  • 10
  • 29