I have discussed that how to use if and else statement in R in my previous article.
In a real time coding, we will be expecting a short form of code which should give the same expected result.
Vectors are basic building block of R Programming and will be used as input.
To apply the same if and else logic for the vector input, we can use if and else function which will give the same result but as a output vector.
The output of the function could be a vector. In the above syntax, test_expression is a condition which we can apply into the input vector and x is nothing but a custom value or expression, in logical it is called TRUE. If the condition is satisfied then x (TRUE) will display, you can replace x with any value. Here y is FALSE, if the test_expression is not satisfied then y will display.
number = c(3,5,7,10)
ifelse(number %% 2 == 0,”even”,”odd”)
Logically the result would be FALSE, FALSE, FALSE, TRUE.