Hi,

I want to use if else statement on each row with loops on given data frame.

ab = data.frame(x = c("P1","P2","P3","P4","P5"),

A = c("100","100","100","100","100"),

B= c("a","b","c","d","e"),

C= c("PR","PR","SR","SR","TR"),

D= c("75","125","NA","NA","NA"),

stringsAsFactors = FALSE)

Some conditions need to apply.I have used loop function but not working..

for(i in 1:nrow(ab)){

if((ab$D != NULL ) & (ab$C == PR) & (ab$D > ab$A))

{

ab$New <- "0"

}

else if((ab$D == NA) & (ab$C == SR))

{

ab$New <- "0"

}

else if((ab$D == NA)&(ab$C == TR))

{

ab$New <- "1"

}

else

{

ab$New<- "No"

}

}

Result should reflect in same data set with new column.

my conditions are:

1)If column d is not null and value of D is greater than column A and value of column c is PR then in new result column there should be print "0".

2)if column d is Null and value of column C is SR then in result column there should be "0".

3)if column d is null and value of column C is TR then in result column there must be "1"

4)Else print "NO"

Need advise.

Desired Output is :

x A B C D NEW

P2 100 b PR 125 0

P3 100 c SR 120 0

P4 100 d SR NA 0

P5. 100 e TR 50 1