Ifelse statement in apply returns unexpected result

I try to use the operator ifelseinside applyand get an odd result. I get the expected answer if the variable markeris equal 1, but not when this variable > 9.

Here is an example dataset for which I am getting the correct answer:

my.data <- read.table(text = '
   REFNO   status    stage   marker   cumulative   newstage
 1018567      ccc       AA        0             1         AA
 1018567      aaa     NONE        0             1       NONE
 1018567      aaa       BB        1             1         BB
 1018567      bbb       CC        1             1         CC
 1018567      eee       CC        1             1         CC
 1018567      mmm       CC        1             1         CC
 1018567      ppp       CC        1             1         CC
 1019711      ddd       CC        1             1         CC
', header = TRUE, stringsAsFactors = FALSE)

my.data$newstage <- apply(my.data, 1, function(x) ifelse(x['status'] == 'aaa'  & 
                                          x['stage']      == 'NONE' & 
                                          x['marker']     == 0      & 
                                          x['cumulative'] > 0, 'BB', x['stage']))

my.data

The dataset below differs in only one element from the above, but I do not get the correct answer.

my.data <- read.table(text = '
   REFNO   status    stage   marker    cumulative   newstage
 1018567      ccc       AA        0             1         AA
 1018567      aaa     NONE        0             1       NONE
 1018567      aaa       BB        1             1         BB
 1018567      bbb       CC        1             1         CC
 1018567      eee       CC        1             1         CC
 1018567      mmm       CC        1             1         CC
 1018567      ppp       CC        1             1         CC
 1019711      ddd       CC       14             1         CC
', header = TRUE, stringsAsFactors = FALSE)

my.data$newstage <- apply(my.data, 1, function(x) ifelse(x['status'] == 'aaa'  & 
                                          x['stage']      == 'NONE' & 
                                          x['marker']     == 0      & 
                                          x['cumulative'] > 0, 'BB', x['stage']))

my.data

Thanks for any suggestions. Perhaps I should use a statement ifinstead if-else?

In particular, I would like to NONEbe replaced by BBfor newstagein the second line.

+4
source share
2 answers

apply(my.data2, 1, function(x) x), marker . - 14. ( ) . " 0" == 0 , FALSE. "0" == 0 TRUE

" 0" == 0
# [1] FALSE
"0" == 0
# [1] TRUE

ifelse , apply. within ( with, akrun), newstage <- ifelse(...)

within(my.data2, {
    newStage <- ifelse(status == "aaa" & stage == "NONE" & marker == 0 & 
                           cumulative > 0, "BB", stage)
})
#     REFNO status stage marker cumulative newstage newStage
# 1 1018567    ccc    AA      0          1       AA       AA
# 2 1018567    aaa  NONE      0          1     NONE       BB
# 3 1018567    aaa    BB      1          1       BB       BB
# 4 1018567    bbb    CC      1          1       CC       CC
# 5 1018567    eee    CC      1          1       CC       CC
# 6 1018567    mmm    CC      1          1       CC       CC
# 7 1018567    ppp    CC      1          1       CC       CC
# 8 1019711    ddd    CC     14          1       CC       CC
+4

, , my.data . , , marker, . , numeric character FALSE.

numeric apply .

my.data$newstage <- apply(my.data, 1, function(x) ifelse(x['status']     == 'aaa'  & 
                                                       x['stage']      == 'NONE' & 
                                                       as.numeric(x['marker'])     == 0      & 
                                                       as.numeric(x['cumulative']) > 0, 'BB', x['stage']))
+3

All Articles