The use of the if conditional statement
The use of a conditional statement or conditional operator is an essential part of any programming task. You may need to test a scenario in which there are two alternative options. Further work will depend on the outcome of the test scenario. In R, using the if
statement, you can compare scenarios that usually have two alternative options. In this recipe, you will use the conditional statement if
.
Getting ready
The general structure of the if
statement in R is as follows:
if(test_scenario){ valid_R_statements ... }
From the general structure of the if
statement, it is intuitive that if the test scenario produces TRUE
, then the statement with curly braces will be executed; otherwise, nothing will happen.
Let’s consider a situation where you have a vector of either numeric or character and you want to check whether there is any missing value or not. If any missing value exists, then you intend to print the position index of the missing...