

Let's quickly create a Data Frame where there is a name of a student as "Student" column with vector as input containing ("Ron", "Jake", "Ava", "Sophia", "Mia"). If Student scores marks above 40, then he/she may be considered 'Pass' otherwise considered 'Fail' in an exam. The example below demonstrates the use of 'ifelse()' function along with the use of a DataFrame. Finally when the individual vector elements is TRUE gets change to 'odd' whereas the 'FALSE' will change to 'even'. The first parameter will form a string vector of c("odd","odd","odd","odd","odd") also the second parameter which in turn will produce string vector as c("even',"even","even","even","even").
RSTUDIO IFELSE CODE
The internal working of code above produces a logical vector as c(FALSE,TRUE,FALSE,TRUE,FALSE). In 'ifelse' function the expression 'v%%2=1' checks the remainder of each number in the 'v' to be one and happens to be TRUE will print "odd" otherwise in FALSE will print "even".

'v' is the vector which consists of a list of numbers. Let's quickly look at an example below where the following code will check whether numbers are odd or even. b: Executes when the logical_expression is FALSE. a: Executes when the logical_expression is TRUE.ģ. logical_expression: Indicates an input vector, which in turn will return the vector of the same size as output.Ģ. The argument above in 'ifelse' states that:ġ. The syntax of 'ifelse()' function in R is done by: All of the vector values are taken as an argument at once rather than taking individual values as an argument multiple times. Also, it uses the 'vectorized' technique, which makes the operation faster. The 'ifelse()' function is the alternative and shorthand form of the R if-else statement.
