Solution Review: ifelse()
In this review, we provide a detailed analysis of the solution to this problem.
We'll cover the following
Solution: Using ifelse()
testVariable <- c (2, 10, 7, 400)cat(ifelse(testVariable %% 2 == 0, "even", "odd"))
Explanation
The testVariable
in this case is a vector of numbers. The method ifelse()
takes a boolean expression and the two statements: “even” and “odd”. The first statement is returned if the expression returns true for that element and if the expression evaluates to false for the given element it returns the second statement.
Remember, the output of this method is a vector where each element is the result of the expression for the corresponding input element.