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 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
.
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") }
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") }
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") }
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") }
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)
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)
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.
Using if-else in R:
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") }
R if-else-if ladder example:
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") }
Nested if-else in R:
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") }
R multiple condition if statements:
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") }
Switch statement in R:
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)
Conditional statements in R:
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") }
R control flow and decision making:
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") }
Comparisons and logical operators in R:
<
, >
, ==
, !=
, &
, |
) 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") }