Question: issue with ifelse statement
1
5 weeks ago
anamaria • 40

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

R • 60 views
ADD COMMENTlink 5 weeks ago anamaria • 40 • updated 5 weeks ago shawn.w.foley • 670
4
5 weeks ago
shawn.w.foley • 670
USA

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$PLASER==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 & PLASER==1.

ADD COMMENTlink 5 weeks ago shawn.w.foley • 670
0

So I did why you posted above and when I do:

 > dim(aa[aa$new==1,])
 [1] 1513    4
> dim(aa[aa$new==2,])
[1] 946   4

while:
> dim(aa)
[1] 1651    4

why do total count of "1" and "2" exceed to number of entries in this data frame?

ADD REPLYlink 5 weeks ago
anamaria
• 40

Login before adding your answer.

Powered by the version 1.4