nkabo nkabo - 3 months ago 5
R Question

Error in if statement in r

This might be a silly question, but since I could not find the way to search it, I wanted to ask. I have the code below:

(m is a numeric value defined earlier)

if (0 < m <= 1.00){
print("a")
} else if (1.00 < m <= 2.00){
print("likely benign")
} else if (2.00 < m <= 3.00){
print("b")
} else if (3.00 < m <= 4.00){
print("c")
} else if (4.00 < m <= 5.00){
print("d")
} else if (5.00 < m <= 6.00){
print("e")
} else{
print("f")
}


It gives the error: Error: unexpected '}' in " }"

but I used if in exactly the same way on the upper part of the code and there is no error from that part. How can I fix this? Thanks in advance.

Answer

We need to change the syntax to &

 if(0 < m & m <= 1.00){
   ---

and also in all the else if conditions.

It is not clear whether m is of length 1 or >1. If it is length 1, use the & and if greater than 1, it may be better to use ifelse instead of if/else or if we are using if/else, use && instead of & as @ZheyuanLi suggested in the comments.