This tutorial explains various ways to apply IF ELSE conditional statements in R. This tutorial covers two ways to handle conditional statements: the ifelse()
function and the if-else
statement. We will also see how IF ELSE statements are used in popular R packages.
ifelse() Function
In this section, we will cover ifelse() Function in detail.
Syntax of ifelse() function
Below is the syntax of the ifelse()
function in R. It works similar to MS Excel IF function.
ifelse(condition, value if condition is true, value if condition is false)
Examples : ifelse Function
Below are examples showing the application of the ifelse() function in R.
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.
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 |
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
Multiple ifelse functions 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)))
Let's dive into the important points regarding the ifelse function, which is commonly used to solve real-world data problems.
How to treat missing values in ifelse Function?
In R, missing values are denoted by the special value NA (Not Available).
Incorrect Methodx = 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.
How to use OR and AND operators in ifelse Function
The & symbol is used to perform AND conditionsifelse(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
How to combine summary and ifelse functions?
In this example, we can counting the number of records where the condition meets. The condition is x1<10 and x2>150.sum(ifelse(mydata$x1<10 | mydata$x2>150,1,0))Result : 7
If Else Statement
There is one more way to define conditional statement in R i.e. if-else statement
. 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-Else Statement
Below is the syntax of the If-Else statement in R.
if(condition) { yes } else { no }Here 'yes' refers to the value when the condition is true, otherwise it refers to 'no'.
It can also be written in a single line like this - if(condition) yes else no
Examples : If..Else If..Else Statements
The code below would return the value 0 because the condition k > 100 is false.
k = 99 if(k > 100) 1 else 0Result : 0
k = 100Result : "Equal to 100"
if(k > 100){
print("Greater than 100")
} else if (k < 100){
print("Less than 100")
} else {
print ("Equal to 100")
}
- The variable
k
is assigned the value 100. Theif
statement first evaluates the conditionk > 100
. Since the value ofk
is not greater than 100, the condition is false. - Next, the
else if
statement is evaluated. The conditionk < 100
is also false because the value ofk
is not less than 100. Since both the initialif
and the subsequentelse if
conditions are false, the control flow moves to theelse
block. - Within the
else
block, the codeprint("Equal to 100")
is executed, resulting in the message "Equal to 100" being printed.
ifelse function vs If-Else Statement
Below are the main differences between the ifelse function and the if-else statement in R.
- Usage: ifelse() function is commonly used when applying a condition to an entire vector or column of data. whereas, if-else statement is typically used when dealing with more complex conditional logic.
- Vectorized vs Scalar: The ifelse() function can work with entire vectors or columns of data at once, while the if-else statement operates on individual values.
- Output Length: The ifelse() function returns a vector with the same length as the input. On the other hand, the if-else statement returns a single value or executes blocks of code.
If Else in Popular Packages
Other than base R, there are functions available in packages for If Else conditions.
dplyr package: If Else
Below is the syntax of if_else( ) function of dplyr package in R.
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 2The %% symbol returns remainder after a value is divided by divisor. In this case, first element 1 is divided by 2.
sqldf package: If Else
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
Operators
Below is a list of operators frequently used in if-else conditions in the R programming language.
- Comparison Operators:
==
: Equal to!=
: Not equal to<
: Less than>
: Greater than<=
: Less than or equal to>=
: Greater than or equal to
- Logical Operators:
&
: Logical AND|
: Logical OR!
: Logical NOT
- Compound Operator :
%in%
: Multiple OR conditions. Checks if a value is in a vector or set
Thanks!
ReplyDeletehow to apply this ( use ifelse with more than one condition0. i want to highlight any brand contain "lifestyle". this code gave me error
ReplyDeletelibrary(dplyr) # for data manipulation
library(tidyr)
df <- df %>%
mutate(Make = row.names(df),
ID = ifelse(names.effectifs %in% c("Lifestyle","Diet Lifestyle","Lifestyle Plus", TRUE, FALSE),
ggplot(df, aes(reorder(names.effectifs, -effectifs),effectifs, fill = ID)) +
geom_bar(stat = "identity") +
coord_flip() +
scale_fill_manual(values = c("grey90", "dodgerblue")) +
annotate("text", x = "Lifestyle", y =-8, label = "Eff=-10", color = "white") +
theme_minimal() +
theme(legend.position = "none")+
ggtitle(" DECREASED | INCREASED")
Hello, great examples thank you.
ReplyDeleteCould you please make an example from this Excel formula? =IF(AT57=1;1;IF(B57=$C$50;1;B57+1))
Thanx
great learning
ReplyDeleteHI , I have three location denoted as 1,2,3 in the data set , i wanted to change the location name 1,2,3 to A, B and 3 respectively . How can i change that in r?
ReplyDelete