Applying multiple if-else conditions on different columns in R

Viewed 299

enter image description hereenter image description hereI have the following dataset:

Column1   Column2   Column3
      3         3         1        
      2         3         2        
      1        NA         2        
     NA         4         1        
      2        NA        NA        
     NA        NA        NA       

I want to create a new column (Column 4) with the following conditions:

  1. If columns 1 and 2 have the same value, the value in column 4 is the same as columns 1 and 2.
  2. If columns 1 and 2 have different values, the value in column 4 should be 5.
  3. If column 1 or column 2 have an NA, pick the value from column 3.
  4. If 2 columns out of 3 have NA, then the value in column 4 should be that of the column that has a non-NA value.
  5. If all the columns have NA, then column 4 should have NA too.
Column1   Column2   Column3   Column4
      3         3         1         3 (Condition 1) 
      2         3         2         5 (Condition 2)
      1        NA         2         2 (Condition 3)
     NA         4         1         1 (Condition 3)
      2        NA        NA         2 (Condition 4)
     NA        NA        NA        NA (Condition 5)

Thanks in advance for answering this query.

1 Answers

How about this?

df <- read.table(text = "Column1   Column2   Column3
      3         3         1        
      2         3         2        
      1        NA         2        
     NA         4         1        
      2        NA        NA        
     NA        NA        NA   ", header = T)


df %>%
  mutate(col4 = case_when(
    is.na(Column1) & is.na(Column2) & is.na(Column3) ~ NA_real_, # Con 5
    is.na(Column1) | is.na(Column2) & !is.na(Column3) ~ as.numeric(Column3), #Con 3
    !is.na(Column1) & is.na(Column2) |is.na(Column3) ~ as.numeric(Column1), #Con4
    Column1 == Column2  ~ as.numeric(Column1), #Con 1
    TRUE ~ 5 #Con 2
  ))

  Column1 Column2 Column3  col4
    <int>   <int>   <int> <dbl>
1       3       3       1     3
2       2       3       2     5
3       1      NA       2     2
4      NA       4       1     1
5       2      NA      NA     2
6      NA      NA      NA    NA

New code

dummy <- data.frame(
  ck6ethrace = c(2,2,3,2,2,2,NA,NA,2,NA,3,NA,1,3,NA,2,NA,2,4,2),
  cm1ethrace = c(2,2,3,1,2,2,2,1,2,2,3,2,1,3,1,2,3,2,4,2),
  cf1ethrace = c(2,2,3,2,2,2,3,1,2,2,2,2,1,3,3,2,3,2,4,2)
)
dummy %>%
  mutate(race = case_when(
    is.na(ck6ethrace) & is.na(cm1ethrace) & is.na(cf1ethrace) ~ NA_real_, # Con 5
    is.na(ck6ethrace) | is.na(cm1ethrace) & !is.na(cf1ethrace) ~ as.numeric(cf1ethrace), #Con 3
    !is.na(ck6ethrace) & is.na(cm1ethrace) |is.na(cf1ethrace) ~ as.numeric(ck6ethrace), #Con4
    ck6ethrace == cm1ethrace  ~ as.numeric(ck6ethrace), #Con 1
    TRUE ~ 5 #Con 2
  ))

result

   ck6ethrace cm1ethrace cf1ethrace race
1           2          2          2    2
2           2          2          2    2
3           3          3          3    3
4           2          1          2    5
5           2          2          2    2
6           2          2          2    2
7          NA          2          3    3
8          NA          1          1    1
9           2          2          2    2
10         NA          2          2    2
11          3          3          2    3
12         NA          2          2    2
13          1          1          1    1
14          3          3          3    3
15         NA          1          3    3
16          2          2          2    2
17         NA          3          3    3
18          2          2          2    2
19          4          4          4    4
20          2          2          2    2
Related