Tutorials
r programming

# IF ELSE Function in R

Learn in detail about the ifelse() function, including syntax, along with finding whether a number is odd or even, and finally, with an example to see whether a student passed or failed their exam.

The 'ifelse()' function is the alternative and shorthand form of the R if-else statement. Also, it uses the 'vectorized' technique, which makes the operation faster. All of the vector values are taken as an argument at once rather than taking individual values as an argument multiple times.

### Syntax

The syntax of 'ifelse()' function in R is done by:
ifelse(logical_expression, a , b)

The argument above in 'ifelse' states that:
1.logical_expression: Indicates an input vector, which in turn will return the vector of the same size as output.
2. a: Executes when the logical_expression is TRUE.
3. b: Executes when the logical_expression is FALSE.

### Example (Odd or Even)

Let's quickly look at an example below where the following code will check whether numbers are odd or even. 'v' is the vector which consists of a list of numbers. 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 = c(14,7,6,9,2)
ifelse(v %% 2 == 1,"odd","even")

1. 'even'
2. 'odd'
3. 'even'
4. 'odd'
5. 'even'

The above code gives the output as:
'even' 'odd' 'even' 'odd' 'even'
The internal working of code above produces a logical vector as c(FALSE,TRUE,FALSE,TRUE,FALSE). 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"). Finally when the individual vector elements is TRUE gets change to 'odd' whereas the 'FALSE' will change to 'even'.

### Example(Pass or Fail)

The example below demonstrates the use of 'ifelse()' function along with the use of a DataFrame. If Student scores marks above 40, then he/she may be considered 'Pass' otherwise considered 'Fail' in an exam.

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"). Another column is "Marks", which contains a vector input with the value as (35,75,45,30,85). Finally, both columns are combined to form data frames and store to 'x'.

x <- data.frame("Student" = c("Ron","Jake","Ava","Sophia","Mia"),"Marks" = c(35,75,45,30,85))
str(x)

'data.frame':    5 obs. of  2 variables:
$Student: Factor w/ 5 levels "Ava","Jake","Mia",..: 4 2 1 5 3$ Marks  : num  35 75 45 30 85

'data.frame':    5 obs. of  2 variables:
$Student: Factor w/ 5 levels "Ava","Jake","Mia",..: 4 2 1 5 3$ Marks  : num  35 75 45 30 85


The above code shows the structure of a Data Frame using the 'str(x)' where it says there are 5 observations with 2 variables with the respective values of Student and Marks printed out.

x$Result = ifelse(x$"Marks">40,"Pass","Fail")
print(x)


The above code gives the output as below:

    Student Marks Result
1    Ron    35   Fail
2   Jake    75   Pass
3    Ava    45   Pass
4 Sophia    30   Fail
5    Mia    85   Pass


The above code has 'x$Result' as a variable is used to add a new column 'Result' in a Data Frame. The 'ifelse()' function has an expression as 'x$"Marks">40', which checks the vector values from Marks and decides the 'Result' to be 'Pass' when Marks is greater than 40 otherwise the 'Result' is 'Fail'.

## Congratulations

Congratulations, you have made it to the end of this tutorial!

You've learned about R's ifelse() function with is syntax along with it's help in finding whether a number is Odd or Even and finally with its example to see whether a student is Pass or Fail in an exam.