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:


    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))