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.
source
share