Search code examples
if-statementdplyrconditional-statementsmutated

Mutate IF ELSE MULTIPLE COLUMNS


I have a file (df) with two columns (LAB and OID)... I would like to create a new variable on df file: column3(OID_new), using the information of Columns 1(LAB) and 2 (OID):

LAB      OID OID_new
12       NA    12
13       NA    13
14       NA    14
plate_1  18    18
plate_2  24    24
Plate_3  23    23

for this, i tried:

df %>%
 mutate(OID_new= ifelse(OID == NA,df$LAB,
                    ifelse(OID ^= NA,"df$OID")))-> df1

But don't work

Thanks in advance


Solution

  • You can achieve that by using ifelse(), as you tried. A few things to mention:

    • ifelse() requires three statements:
      1. a test (here: check if OID is NA: is.na(OID))
      2. an output if your test result is TRUE: return LAB
      3. an output if your test result in FALSE: return OID
    • if you pipe %>% your dataframe, there's no need to access your dataframe by using $
    • the base ifelse() is better here, since its dplyr version if_else() is type stable and would throw an error. This might be more convenient, but there might be unexpected results after implicit coercing, so be careful.

    Code

    df %>% mutate(OID_new = ifelse(is.na(OID), LAB, OID)) -> df1
    

    Output

    > df1
          LAB OID OID_new
    1      12  NA      12
    2      13  NA      13
    3      14  NA      14
    4 plate_1  18      18
    5 plate_2  24      24
    6 plate_3  23      23
    

    Data

    df <- structure(list(LAB = c("12", "13", "14", "plate_1", "plate_2", "plate_3"), 
                         OID = c(NA, NA, NA, 18, 24, 23)), 
                    class = "data.frame", 
                    row.names = c(NA, -6L))