R Tutorial

Fundamentals of R

Variables

Input and Output

Decision Making

Control Flow

Functions

Strings

Vectors

Lists

Arrays

Matrices

Factors

DataFrames

Object Oriented Programming

Error Handling

File Handling

Packages in R

Data Interfaces

Data Visualization

Statistics

Machine Learning with R

Decision Making - if, if-else, if-else-if ladder, nested if-else, and switch in R

Decision-making structures in R allow you to specify that a particular action is to be taken upon a certain condition. In this tutorial, we will cover the common decision-making structures in R: if, if-else, if-else-if ladder, nested if-else, and switch.

1. if statement:

The if statement evaluates the test expression inside the parenthesis. If the test expression is TRUE, the statements inside the curly braces {} are executed.

x <- 3
if (x > 2) {
  print("x is greater than 2")
}

2. if-else statement:

The if-else statement provides an alternative choice when if condition is FALSE.

x <- 1
if (x > 2) {
  print("x is greater than 2")
} else {
  print("x is not greater than 2")
}

3. if-else-if ladder:

The if-else-if ladder is used when multiple conditions are to be checked.

x <- 5
if (x == 1) {
  print("x is 1")
} else if (x == 2) {
  print("x is 2")
} else {
  print("x is neither 1 nor 2")
}

4. nested if-else:

You can use one if-else or if statement inside another if or if-else statement(s).

x <- 3
y <- 4
if (x == 3) {
  if (y == 4) {
    print("x is 3 and y is 4")
  } else {
    print("x is 3 but y is not 4")
  }
} else {
  print("x is not 3")
}

5. switch:

The switch statement can be used as a replacement for lengthy if-else-if ladders when comparing one value against multiple values.

value <- "two"

result <- switch(value,
  "one" = "Value is one",
  "two" = "Value is two",
  "three" = "Value is three",
  "Value is something else"
)

print(result)

Another use of switch is for evaluating expressions:

operation <- "+"
a <- 5
b <- 3

result <- switch(operation,
  "+" = a + b,
  "-" = a - b,
  "*" = a * b,
  "/" = a / b,
  "Invalid operation"
)

print(result)

Conclusion:

Decision-making structures in R provide flexibility in executing specific blocks of code based on conditions. It's a fundamental concept in programming, and understanding these structures in R helps in writing efficient and effective code. Remember that conditions are evaluated based on logical expressions, and the outcome (TRUE or FALSE) determines the flow of execution.

  1. Using if-else in R:

    • The if-else statement is used for basic decision making in R.
    # Using if-else in R
    x <- 10
    
    if (x > 5) {
      print("x is greater than 5")
    } else {
      print("x is not greater than 5")
    }
    
  2. R if-else-if ladder example:

    • You can use an if-else-if ladder to check multiple conditions.
    # Using if-else-if ladder in R
    x <- 10
    
    if (x > 10) {
      print("x is greater than 10")
    } else if (x == 10) {
      print("x is equal to 10")
    } else {
      print("x is less than 10")
    }
    
  3. Nested if-else in R:

    • Nested if-else statements allow for more complex decision structures.
    # Using nested if-else in R
    x <- 10
    y <- 5
    
    if (x > 5) {
      if (y > 3) {
        print("Both conditions are true")
      } else {
        print("Inner condition is false")
      }
    } else {
      print("Outer condition is false")
    }
    
  4. R multiple condition if statements:

    • Combine multiple conditions in a single if statement using logical operators.
    # Using multiple conditions in if statement
    x <- 10
    y <- 5
    
    if (x > 5 & y > 3) {
      print("Both conditions are true")
    } else {
      print("At least one condition is false")
    }
    
  5. Switch statement in R:

    • The switch statement allows you to select from multiple alternatives based on a variable's value.
    # Using switch statement in R
    day <- "Monday"
    
    result <- switch(
      day,
      "Monday" = "Start of the week",
      "Friday" = "End of the week",
      "Default case"
    )
    
    print(result)
    
  6. Conditional statements in R:

    • Conditional statements, including if-else, switch, and logical operators, help control the flow of the program.
    # Using conditional statements in R
    x <- 10
    
    if (x > 5 & x < 15) {
      print("x is between 5 and 15")
    } else {
      print("x is outside the specified range")
    }
    
  7. R control flow and decision making:

    • Control flow in R is managed using decision-making structures like if-else and switch.
    # Control flow and decision making in R
    x <- 10
    
    if (x %% 2 == 0) {
      print("x is an even number")
    } else {
      print("x is an odd number")
    }
    
  8. Comparisons and logical operators in R:

    • Logical operators (<, >, ==, !=, &, |) are used in comparisons and decision-making structures.
    # Using comparisons and logical operators in R
    x <- 10
    y <- 5
    
    if (x > y & x %% 2 == 0) {
      print("Conditions are true")
    } else {
      print("At least one condition is false")
    }