issue with ifelse statement
2
1
Entering edit mode
4.9 years ago
anamaria ▴ 220

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 • 842 views
ADD COMMENT
4
Entering edit mode
4.9 years ago
shawn.w.foley ★ 1.3k

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.

ADD COMMENT
0
Entering edit mode

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 REPLY
0
Entering edit mode

This is wrong, where is the condition to check "RTNPTHY" column?

ADD REPLY
0
Entering edit mode

Apologies for the Typo, I misread column names in the OP, I've edited my answer.

ADD REPLY
2
Entering edit mode
4.9 years ago
zx8754 11k

It is a typo, your code should just work fine, check your column name RTNPTHY vs aa$RTNPHY in you ifelse statement.

ADD COMMENT

Login before adding your answer.

Traffic: 2108 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6