Hi,

I have a file like this:

    > head(aa)
      PLASER CURRELIG RTNPTHY
    1      1        1       1
    2      1        1       1
    3      2        1       2
    4      1        1       2
    5      1        1       2
    6      1        1       2

I have to create another column, say "new" which will satisfy these conditions:

    PLASER=2 AND CURRELIG=1  -> new=2
    RTNPHY=1 AND CURRELIG=1    -> new=1

I tried running this command:

     aa$new <- ifelse(aa$CURRELIG==1 & aa$PLASER==2, 2, ifelse(aa$CURRELIG==1 & aa$RTNPHY==1,1,NA))

but I am getting this output which is not correct:

    > head(aa)
      PLASER CURRELIG RTNPTHY new
    1      1        1       1  NA
    2      1        1       1  NA
    3      2        1       2   2
    4      1        1       2  NA
    5      1        1       2  NA
    6      1        1       2  NA

Can someone please help with this?

Thanks
ANA


It looks like the `ifelse` command isn't meant to be nested like you have in your code, it's for binary output and it seem like you want three options (`1`, `2`, or `NA`). From the man page:

    Usage:
         ifelse(test, yes, no)
    Arguments:
        test: an object which can be coerced to logical mode.
        yes: return values for true elements of ‘test’.
        no: return values for false elements of ‘test’.

It might be easier to run:

    aa$new <- NA
    aa$new[aa$CURRELIG==1 & aa$PLASER==2] <- 2
    aa$new[aa$CURRELIG==1 & aa$RTNPHY==1] <- 1

This will generate a new column of `NAs`, then replace the `NAs` with `1` when both `CURRELIG==1 & PLASER==2` or replace them with `2` when both `CURRELIG==1 & RTNPHY==1`.