This tutorial explains various ways to apply IF ELSE conditional statements in R, along with examples.
There are the following two ways to handle conditional statements in R.
ifelse()
functionif-else
statement
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)Example
my_variable <- c(-2, 0, 5, -3, 2, 6) result <- ifelse(my_variable > 0, "Positive", "Non-Positive")
[1] "Non-Positive" "Non-Positive" "Positive" "Non-Positive"
[5] "Positive" "Positive"
Below is the syntax of the if-else statement in R.
if (condition1) { # Code to be executed if condition1 is TRUE } else if (condition2) { # Code to be executed if condition2 is TRUE and condition1 is FALSE } else { # Code to be executed if both condition1 and condition2 are FALSE }Example
my_variable <- -7 if (my_variable > 0) { print("it is positive.") } else if (my_variable < 0) { print("it is negative.") } else { print("it is zero.") }
[1] "it is negative."
ifelse() Function
In this section, we will cover ifelse() Function in detail.
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 syntax 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])
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.
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
The following code sets a variable k to 100. It checks if k is greater than 100. Since it's not, it checks if k is less than 100 which is also false. So it prints "Equal to 100".
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"
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 R Packages
Other than base R, there are functions available in packages for If Else conditions.
dplyr: 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)
Example 1: Using if_else() to create a new column.
The following program creates a new column called "SpeciesType" based on the condition that if the "Species" column is equal to "setosa", the corresponding "SpeciesType" value is set to "Type A". Otherwise it's set to "Type B".
iris <- iris %>% mutate(SpeciesType = if_else(Species == "setosa", "Type A", "Type B")) unique(iris$SpeciesType)Result :
[1] "Type A" "Type B"
Example 2: 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: If Else
We can write SQL query in R using sqldf package. In SQL, If Else statement is defined in CASE WHEN statement.
df=data.frame(var1=c(2,NA,3,4,5)) library(sqldf) sqldf( "SELECT *, CASE WHEN (var1%2)=0 THEN 'Multiple of 2' WHEN var1 is NULL THEN 'Missing' ELSE 'Not a multiple of 2' END AS new_variable FROM df" )Output
var1 new_variable
1 2 Multiple of 2
2 NA Missing
3 3 Not a multiple of 2
4 4 Multiple of 2
5 5 Not a multiple of 2
Operators
Below is a list of operators frequently used in if-else conditions in R.
- 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