R Control Statements

R Control Statements

Tutorial Name: Codes With Pankaj Website: www.codeswithpankaj.com


Table of Contents

  1. Introduction to Control Statements

  2. Conditional Statements

    • if Statement

    • if...else Statement

    • ifelse() Function

    • Nested if...else

  3. Switch Statement

  4. Looping Statements

    • for Loop

    • while Loop

    • repeat Loop

  5. Break and Next Statements


1. Introduction to Control Statements

Control statements in R allow you to control the flow of your program by executing specific code blocks based on conditions or by iterating over data structures. These statements are essential for decision-making, looping, and managing code execution.


2. Conditional Statements

Conditional statements are used to perform different actions based on different conditions.

2.1 if Statement

The if statement executes a block of code if the specified condition is TRUE.

Syntax:

if (condition) {
  # Code to execute if condition is TRUE
}

Example:

x <- 10

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

2.2 if...else Statement

The if...else statement allows you to execute one block of code if the condition is TRUE and another block if the condition is FALSE.

Syntax:

if (condition) {
  # Code to execute if condition is TRUE
} else {
  # Code to execute if condition is FALSE
}

Example:

x <- 3

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

2.3 ifelse() Function

The ifelse() function is a vectorized version of the if...else statement. It checks a condition and returns one value if TRUE and another if FALSE.

Syntax:

ifelse(test, yes, no)

Example:

x <- 10
y <- ifelse(x > 5, "Greater", "Not Greater")
print(y)  # Output: "Greater"

2.4 Nested if...else

You can nest multiple if...else statements to check multiple conditions.

Example:

x <- 10

if (x > 20) {
  print("x is greater than 20")
} else if (x > 10) {
  print("x is greater than 10 but less than or equal to 20")
} else {
  print("x is 10 or less")
}

3. Switch Statement

The switch statement evaluates an expression and executes a block of code based on the value of that expression.

Syntax:

switch(expression, case1, case2, case3, ...)

Example:

day <- 2

result <- switch(day,
  "Sunday",
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday"
)

print(result)  # Output: "Monday"

4. Looping Statements

Looping statements allow you to repeat a block of code multiple times.

4.1 for Loop

The for loop is used to iterate over a sequence (e.g., a vector or list).

Syntax:

for (variable in sequence) {
  # Code to execute
}

Example:

for (i in 1:5) {
  print(i)
}

4.2 while Loop

The while loop repeats a block of code as long as the specified condition is TRUE.

Syntax:

while (condition) {
  # Code to execute
}

Example:

x <- 1

while (x <= 5) {
  print(x)
  x <- x + 1
}

4.3 repeat Loop

The repeat loop executes a block of code repeatedly until a break statement is encountered.

Syntax:

repeat {
  # Code to execute
  if (condition) {
    break
  }
}

Example:

x <- 1

repeat {
  print(x)
  x <- x + 1
  if (x > 5) {
    break
  }
}

5. Break and Next Statements

5.1 Break Statement

The break statement is used to exit a loop prematurely when a certain condition is met.

Example:

for (i in 1:10) {
  if (i == 5) {
    break
  }
  print(i)
}

5.2 Next Statement

The next statement is used to skip the current iteration of a loop and move to the next iteration.

Example:

for (i in 1:5) {
  if (i == 3) {
    next
  }
  print(i)
}

Conclusion

Control statements are fundamental in R programming as they allow you to control the flow of execution and make decisions in your code. By mastering these statements, you can create more complex and dynamic R programs.

For more tutorials and resources, visit Codes With Pankaj at www.codeswithpankaj.com.

Last updated