R : If Else and Nested If Else

If-Else and Nested If-Else in R

The If-Else statements are important part of R programming. In this tutorial, we will see various ways to apply conditional statements (If..Else nested IF) in R. In R, there are a lot of powerful packages for data manipulation. In the later part of this tutorial, we will see how IF ELSE statements are used in popular packages.

Sample Data

Let's create a sample data to show how to perform IF ELSE function. This data frame would be used further in examples.
x1 x2 x3
1 129 A
3 178 B
5 140 C
7 186 D
9 191 E
11 104 F
13 150 G
15 183 H
17 151 I
19 142 J

Run the program below to generate the above table in R.
set.seed(123)
mydata = data.frame(x1 = seq(1,20,by=2),
                    x2 = sample(100:200,10,FALSE),
                    x3 = LETTERS[1:10])
x1 = seq(1,20,by=2) : The variable 'x1' contains alternate numbers starting from 1 to 20. In total, these are 10 numeric values.

x2 = sample(100:200,10,FALSE) : The variable 'x2' constitutes 10 non-repeating random numbers ranging between 100 and 200.

x3 = LETTERS[1:10] : The variable 'x3' contains 10 alphabets starting from A to Z.

Syntax of ifelse() function :

The ifelse() function in R works similar to MS Excel IF function. See the syntax below -
ifelse(condition, value if condition is true, value if condition is false)
Example 1 : Simple IF ELSE Statement

Suppose you are asked to create a binary variable - 1 or 0 based on the variable 'x2'. If value of a variable 'x2' is greater than 150, assign 1 else 0.
mydata$x4 = ifelse(mydata$x2>150,1,0)
In this case, it creates a variable x4 on the same data frame 'mydata'. The output is shown in the image below -
ifelse : Output
Create variable in a new data frame

Suppose you need to add the above created binary variable in a new data frame. You can do it by using the code below -
x = ifelse(mydata$x2>150,1,0)
newdata = cbind(x,mydata)
The cbind() is used to combine two vectors, matrices or data frames by columns.

Apply ifelse() on Character Variables

If variable 'x3' contains character values - 'A', 'D', the variable 'x1' should be multiplied by 2. Otherwise it should be multiplied by 3.
mydata$y = ifelse(mydata$x3 %in% c("A","D") ,mydata$x1*2,mydata$x1*3)
The output is shown in the table below

x1 x2 x3 y
1 129 A 2
3 178 B 9
5 140 C 15
7 186 D 14
9 191 E 27
11 104 F 33
13 150 G 39
15 183 H 45
17 151 I 51
19 142 J 57

Example 2 : Nested If ELSE Statement in R

Multiple If Else statements can be written similarly to excel's If function. In this case, we are telling R to multiply variable x1 by 2 if variable x3 contains values 'A' 'B'. If values are 'C' 'D', multiply it by 3. Else multiply it by 4.
mydata$y = ifelse(mydata$x3 %in% c("A","B") ,mydata$x1*2,
                  ifelse(mydata$x3 %in% c("C","D"), mydata$x1*3,
                         mydata$x1*4))
Do you hate specifying data frame multiple times with each variable?

You can use with() function to avoid mentioning data frame each time. It makes writing R code faster.
mydata$y = with(mydata, ifelse(x3 %in% c("A","B") , x1*2,
                  ifelse(x3 %in% c("C","D"), x1*3, x1*4)))

Special Topics related to IF ELSE

In this section, we will cover the following topics -

  1. How to treat missing (NA) values in IF ELSE.
  2. How to use OR and AND operators in IF ELSE
  3. Aggregate or Summary Functions and IF ELSE Statement

Handle Missing Values

Incorrect Method
x = NA
ifelse(x==NA,1,0)
Result :  NA
It should have returned 1.

Correct Method 
x = NA
ifelse(is.na(x),1,0)
Result : 1
The is.na() function tests whether a value is NA or not.

Use OR and AND Operators

The & symbol is used to perform AND conditions
ifelse(mydata$x1<10 & mydata$x2>150,1,0)
Result : 0 1 0 1 1 0 0 0 0 0

The | symbol is used to perform OR conditions
ifelse(mydata$x1<10 | mydata$x2>150,1,0)
Result : 1 1 1 1 1 0 0 1 1 0

Count cases where condition meets

In this example, we can counting the number of records where the condition meets.
sum(ifelse(mydata$x1<10 | mydata$x2>150,1,0))
Result : 7

If Else Statement : Another Style

There is one more way to define if..else statement in R. This style of writing If Else is mostly used when we use conditional statements in loop and R functions. In other words, it is used when we need to perform various actions based on a condition.

Syntax -
if(condition) yes else no
k = 99
if(k > 100) 1 else 0
Result : 0

If..Else If..Else Statements
k = 100
if(k > 100){
  print("Greater than 100")
} else if (k < 100){
  print("Less than 100")
} else {
  print ("Equal to 100")
}
Result : "Equal to 100"

If Else in Popular Packages

1. dplyr package

if_else(condition, value if condition is true, value if condition is false, value if NA)

The following program checks whether a value is a multiple of 2
library(dplyr)
x=c(1,NA,2,3)
if_else(x%%2==0, "Multiple of 2", "Not a multiple of 2", "Missing")
Result : "Not a multiple of 2" "Missing" "Multiple of 2" "Not a multiple of 2"

The %% symbol returns remainder after a value is divided by divisor. In this case, first element 1 is divided by 2.

2. sqldf package

We can write SQL query in R using sqldf package. In SQL, If Else statement is defined in CASE WHEN.
df=data.frame(k=c(2,NA,3,4,5))
library(sqldf)
sqldf(
  "SELECT *,
  CASE WHEN (k%2)=0  THEN 'Multiple of 2'
  WHEN  k is NULL  THEN 'Missing'
  ELSE 'Not a multiple of 2'
  END AS T
  FROM df"
)
Output

k T
2 Multiple of 2
NA Missing
3 Not a multiple of 2
4 Multiple of 2
5 Not a multiple of 2 
Love this Post? Spread the Word!
Comment and share to motivate us to write more!
About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 8 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Telecom and Human Resource.

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*
Related Posts:
3 Responses to "R : If Else and Nested If Else"
  1. We shall know what food is perfect for humans based on two factors-What goes well with the human body & natural features.
    1. See, bodily, human body consists of over 70% water and remaining 30% is solids and gases! Thus, at all the solid food we take, it must be having 70% of water (at least closer to that)! Such food will speedily and easily be included with our system. The more difference from this take of water, the more risky such food becomes! On this fact, the next foods are to eat/not to eat;
    YES:
    All the fruits, vegetables, leafy vegetables, cooked rice, soup items, cooked in more water & less oil-items.
    NO:
    All the fry items, dry items (except for dry fruits), roasted items, cooked in more oil & less water items, meat (apart from fish & egg), flour made items.
    2. The human living is all about know-how the Life. A person cans knowledge time in more deeper and vast way only when his system is responsive to Life. System will be 100% aware to days only when his body-Mind, Heart, Sense Organs, Nerve System and other body parts all work in a impartial way. But there are certain food items that will act only on nerve system, kindle it to its peak and make other organs dumb. Due to this, for the moment, rest of the body and energy of the mind is felt. Some feel it like a stress reliever and get keen to those items. But actually, what is happening with it is it is inspiring the nerve system and making all other organs in the body dumb. Hence, slowly, such person will lose kindliness towards the Life and his skill of Life goes down. Those food items are not to be eaten (may be once in a while just by choice is okay but a regular use or need will lead to decreased kindness towards the Life);
    NO:
    Drugs, Alcohol, Cigarette, Tea, Coffee, Chocolates, Masalas, Garlic, Onions, Green Chillis, Green Pepper, Red Pepper.for more information visit cheap essay writing service

    ReplyDelete

We have Zero Tolerance to Spam. Comments with links will be deleted immediately upon our review.

Next → ← Prev
Scroll to Top