How to use if else in R

base-r
if else
Learn how to perform use if else in R. Step-by-step statistical tutorial with examples.
Published

February 22, 2026

Introduction

The if else statement is one of the most fundamental control structures in R programming. It allows you to execute different code blocks based on whether specific conditions are true or false. Use if else statements when you need to make decisions in your code, such as categorizing data, handling missing values, or applying different calculations based on variable values.

Getting Started

library(tidyverse)
library(palmerpenguins)

Example 1: Basic Usage

The Problem

We need to understand the basic syntax of if else statements and how they evaluate logical conditions. Let’s start with simple numeric comparisons to see how R makes decisions.

Step 1: Simple if statement

Test a basic condition with a single if statement.

x <- 10

if (x > 5) {
  print("x is greater than 5")
}

The condition x > 5 evaluates to TRUE, so the code inside the braces executes and prints the message.

Step 2: Adding an else clause

Provide an alternative action when the condition is false.

y <- 3

if (y > 5) {
  print("y is greater than 5")
} else {
  print("y is 5 or less")
}

Since y equals 3, the condition is FALSE, so R executes the else block and prints the second message.

Step 3: Multiple conditions with else if

Handle multiple scenarios using else if statements.

score <- 85

if (score >= 90) {
  grade <- "A"
} else if (score >= 80) {
  grade <- "B"
} else {
  grade <- "C"
}

print(paste("Grade:", grade))

R evaluates conditions sequentially, finds that score >= 80 is TRUE, assigns “B” to grade, and skips the remaining conditions.

Example 2: Practical Application

The Problem

We want to categorize penguins from the Palmer Penguins dataset based on their body mass. Specifically, we need to create weight categories and handle any missing values that might exist in our data.

Step 1: Load and examine the data

First, let’s look at the body mass variable to understand our data.

data(penguins)

# Check the range of body mass values
summary(penguins$body_mass_g)

# Look at first few rows
head(penguins$body_mass_g)

This shows us the distribution of penguin weights and reveals if there are any missing values we need to handle.

Step 2: Create a categorization function

Build a function that uses if else to categorize penguin weights.

categorize_weight <- function(mass) {
  if (is.na(mass)) {
    return("Unknown")
  } else if (mass < 3500) {
    return("Light")
  } else if (mass < 4500) {
    return("Medium")
  } else {
    return("Heavy")
  }
}

This function first checks for missing values, then categorizes valid weights into three groups based on gram thresholds.

Step 3: Apply the function to create new variable

Use our function to add a weight category column to the dataset.

penguins_categorized <- penguins |>
  mutate(weight_category = map_chr(body_mass_g, categorize_weight))

# Check the results
table(penguins_categorized$weight_category)

The map_chr() function applies our categorization to each penguin, and table() shows how many penguins fall into each category.

Step 4: Vectorized approach with ifelse

Create the same categorization using vectorized ifelse for better performance.

penguins_vectorized <- penguins |>
  mutate(
    weight_category = ifelse(is.na(body_mass_g), "Unknown",
                      ifelse(body_mass_g < 3500, "Light",
                      ifelse(body_mass_g < 4500, "Medium", "Heavy")))
  )

# Verify results match
identical(penguins_categorized$weight_category, 
          penguins_vectorized$weight_category)

The vectorized ifelse() function processes all rows simultaneously, making it more efficient than applying a custom function row by row.

Summary

  • Use if else statements for conditional logic when you need different actions based on true/false conditions
  • Basic syntax includes if (condition) { action } with optional else { alternative } blocks
  • Chain multiple conditions using else if to handle complex decision trees
  • For data analysis, consider vectorized ifelse() function for better performance on large datasets
  • Always handle missing values (NA) explicitly in your conditional statements to avoid unexpected errors