Question: issue with ifelse statement
1
gravatar for anamaria
17 months ago by
anamaria110
anamaria110 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 • 293 views
ADD COMMENTlink modified 17 months ago by Santosh Anand5.2k • written 17 months ago by anamaria110
4
gravatar for shawn.w.foley
17 months ago by
shawn.w.foley1.2k
USA
shawn.w.foley1.2k 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 17 months ago • written 17 months ago by shawn.w.foley1.2k

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 17 months ago • written 17 months ago by anamaria110

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

ADD REPLYlink written 17 months ago by zx87549.7k

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

ADD REPLYlink written 17 months ago by shawn.w.foley1.2k
2
gravatar for zx8754
17 months ago by
zx87549.7k
London
zx87549.7k 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 17 months ago by zx87549.7k
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: 1002 users visited in the last hour