Conditional Statements | Dataplexa

Conditional Statements in R

In this lesson, you will learn how R makes decisions using conditional statements. Conditional statements allow a program to choose different actions based on conditions.

They are extremely important because real-world programs must respond differently depending on data values, user input, or logical checks.


What Is a Conditional Statement?

A conditional statement checks whether a condition is TRUE or FALSE. Based on the result, R executes a specific block of code.

Without conditional statements, programs would always behave the same way, which is not practical for real applications.


The if Statement

The if statement executes code only when a condition is true.

If the condition is false, the code inside the if block is skipped.

age <- 20

if (age >= 18) {
  print("Eligible to vote")
}

Here, the message is printed only if the condition is satisfied.


The if...else Statement

The if...else statement provides an alternative path when the condition is false.

This allows programs to handle both outcomes clearly.

marks <- 45

if (marks >= 50) {
  print("Pass")
} else {
  print("Fail")
}

Only one block will execute depending on the condition.


The if...else if...else Ladder

When multiple conditions need to be checked, R uses an else if ladder.

Conditions are evaluated from top to bottom until one becomes true.

score <- 82

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

print(grade)

This structure is widely used in grading systems and evaluations.


Nested if Statements

An if statement can be placed inside another if block.

Nested conditions are useful when multiple rules depend on each other.

age <- 25
citizen <- TRUE

if (age >= 18) {
  if (citizen == TRUE) {
    print("Eligible to apply")
  } else {
    print("Citizenship required")
  }
}

This ensures all required conditions are checked correctly.


Using Logical Operators in Conditions

Logical operators allow you to combine multiple conditions in one statement.

They make decision-making more powerful and flexible.

age <- 30
experience <- 5

if (age >= 25 & experience >= 3) {
  print("Eligible for senior role")
}

Both conditions must be true for the code to execute.


The ifelse() Function

R provides a vectorized conditional function called ifelse().

It is commonly used for applying conditions to vectors or columns.

scores <- c(45, 72, 88)

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

This function is faster and cleaner for large datasets.


Conditional Statements with Data Frames

Conditions are often applied to data frame columns.

This helps classify or filter data based on rules.

df <- data.frame(score = c(55, 78, 92))

df$grade <- ifelse(df$score >= 60, "Pass", "Fail")
df

Conditional logic helps enrich datasets with meaningful labels.


Why Conditional Statements Matter

Conditional statements help programs react intelligently to data.

They are used in validation, grading, filtering, automation, and decision systems.


📝 Practice Exercises


Exercise 1

Write an if statement to check whether a number is positive.

Exercise 2

Create an if...else statement to check if a person is eligible to vote.

Exercise 3

Use an if...else if ladder to assign grades based on marks.

Exercise 4

Use ifelse() to label values in a vector as “High” or “Low”.


✅ Practice Answers


Answer 1

num <- 5

if (num > 0) {
  print("Positive number")
}

Answer 2

age <- 17

if (age >= 18) {
  print("Eligible to vote")
} else {
  print("Not eligible")
}

Answer 3

marks <- 68

if (marks >= 90) {
  grade <- "A"
} else if (marks >= 75) {
  grade <- "B"
} else if (marks >= 60) {
  grade <- "C"
} else {
  grade <- "D"
}

grade

Answer 4

values <- c(40, 80, 60)

label <- ifelse(values >= 60, "High", "Low")
label

What’s Next?

In the next lesson, you will learn about loops in R.

Loops allow programs to repeat tasks efficiently and process data at scale.