Learn R Online
 Home            

R ifelse( ) Function

In this tutorial you will learn about ifelse() function and how it works on vectors. Actually this function just works like if...else statement but it is a shorter version which works on a vector. 

The real power of R programming is in vectorization and infact this concept differentiates R from other most of programming languages. If you have a list of data, then instead of applying certain operation on each data item individually with the help of loop or any other iterative structure you apply the operation on the vector as a whole. There is no need to apply function on each item seperately. Just see the example here to understand this idea

We create a vector 

> x <- c(2,4,3,5,7,4,6)

Now if you want to add 2 in each element of vector, instead of adding 2 in each item seperately simply add 2 in x

> x + 2

[1] 4 6 5 7 9 6 8

Here 2 is added in each element. This is how vectorized operations are done.

Vectors are the basic unit of structure and function in R. R functions may take a vector as an argument, do some process and return a vector.

ifelse() function syntax:

The syntax is very simple. This function takes a logical expression test on some vector, if the test is True, the first action True is taken otherwise the second action False is taken. 

ifelse(logical_expression, True, False)

If you notice, this expression is a bit similar to if conditional statement in Microsoft Excel but the difference is that here it is applied on each element of a vector one by one and result is also a vector.

Example: ifelse() function

if there is a list of scores of seven students in math, we create a vector with c() function

> score <- c(77,35,89,100,45,67,50)

Now we want to check the condition if score is greater than or equal to fifty the student is pass, else the student is fail. Using this function on score vector the code is 

> ifelse(score >= 50, "Pass", "Fail")

[1] "Pass" "Fail" "Pass" "Pass" "Fail" "Pass" "Pass"

It can be seen that ifelse takes score vector and performs the test one by one on each element of the vector and produces output in the form of a vector. This resultant vector can be assigned to some variable. 

> result <- ifelse(score >= 50, "Pass", "Fail")

> result

[1] "Pass" "Fail" "Pass" "Pass" "Fail" "Pass" "Pass"

Now lets see the difference if you use if...else statement for the same test condition then how it will be done. Ofcourse, a loop will be needed and the logical test is applied on each element seperately in the code.

> for ( element in score ){
+ if(element >= 50) print("Pass")
+ else print("Fail")
+ }
[1] "Pass"
[1] "Fail"
[1] "Pass"
[1] "Pass"
[1] "Fail"
[1] "Pass"
[1] "Pass"

One can say this function is vectorised form of simple if...else statement which is more readable and efficient.

In another example we check either a number is even or odd. First we create a vector and name it as number, then we check if an element of vector is divisible by 2. %% is used to find out the remainder of a division operation. number %%2 will return the remainder of number/2. 

if that remainder is zero, it will mean the number is divisible by 2 and hence it is an Even number, otherwise the number is odd. The R code for this condition will be

> number <- c(3,2,8,6,9,7,10)
> ifelse(number %% 2 == 0, "Even","Odd")
[1] "Odd"  "Even" "Even" "Even" "Odd"  "Odd"  "Even"

I hope that the idea is clear now. Always remember, practice makes a programmer perfect, hence try to write a few programs in R about this idea.

Learn to Shine

  • Programming made Easy for all
  • High School and College
  • University level
  • Professionals doctors, engineers, scientists
  • Data Analysts