Reputation: 1
I am executing following code in R using "IF" and the condition executed (I was expecting, it will give error message),
if("TRUE") print("ok")
can some one help me in understanding the logic behind the code execution?
My understanding is that "if statement" will execute when the conditional expression is true.
In the above code, I have given character as input, but the if condition is executed, which surprise me.
Upvotes: 0
Views: 200
Reputation: 2864
R converts the argument of if
statement if it is interpretable as logical. In this case "TRUE"
is interpretable as logical. Please see that as.logical("TRUE")
returns TRUE
. However, if("HELLO") print("ok")
would not work and you will get the error:
Error in if ("HELLO") print("ok") :
argument is not interpretable as logical
Upvotes: 1
Reputation: 712
You just need to fix the error in your syntax. Try this:
if (TRUE){
print("ok")
}
Upvotes: 0