Question: issue with ifelse statement
1
gravatar for anamaria
3 days ago by
anamaria40
anamaria40 wrote:

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 • 131 views
ADD COMMENTlink modified 2 days ago by Santosh Anand4.8k • written 3 days ago by anamaria40
4
gravatar for shawn.w.foley
3 days ago by
shawn.w.foley710
USA
shawn.w.foley710 wrote:

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 COMMENTlink modified 2 days ago • written 3 days ago by shawn.w.foley710

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 modified 3 days ago • written 3 days ago by anamaria40

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

ADD REPLYlink written 2 days ago by zx87547.5k

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

ADD REPLYlink written 2 days ago by shawn.w.foley710
2
gravatar for zx8754
2 days ago by
zx87547.5k
London
zx87547.5k wrote:

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

ADD COMMENTlink written 2 days ago by zx87547.5k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 554 users visited in the last hour