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
Control statements in programming are used to dictate the flow of execution based on certain conditions or loops. R, like other programming languages, offers a suite of control statements to facilitate this. This tutorial will cover the basics of control statements in R, including conditional statements and loops.
if
statement:The basic if
statement checks a condition and executes the associated code block if the condition is TRUE
.
x <- 5 if (x > 0) { print("x is positive") }
if-else
statement:Adds an alternative action if the initial if
condition is FALSE
.
if (x > 0) { print("x is positive") } else { print("x is non-positive") }
if-else if-else
statement:Allows checking multiple conditions sequentially.
if (x > 0) { print("x is positive") } else if (x < 0) { print("x is negative") } else { print("x is zero") }
The switch
statement is used to execute one block of code from multiple conditions:
y <- "b" result <- switch(y, a = "You chose A", b = "You chose B", c = "You chose C", "Unknown choice" ) print(result)
for
loop:Used for iterating over a sequence (vector, list).
for (i in 1:5) { print(i) }
while
loop:Keeps running as long as the condition is TRUE
.
count <- 1 while (count <= 5) { print(count) count <- count + 1 }
repeat
loop:An infinite loop that only breaks when a break
statement is encountered.
count <- 1 repeat { print(count) if (count >= 5) { break } count <- count + 1 }
next
:Skips the current iteration and proceeds to the next.
for (i in 1:5) { if (i == 3) { next } print(i) }
break
:Exits the loop immediately.
for (i in 1:5) { if (i == 4) { break } print(i) }
Control statements in R, such as conditional statements (if-else
) and loops (for
, while
, and repeat
), offer flexibility in code execution based on specified conditions. By understanding and using these constructs effectively, you can create dynamic and adaptable R scripts.
If-Else Statements in R:
Use if-else statements for conditional execution.
# If-else statements in R x <- 10 if (x > 0) { print("Positive") } else { print("Non-positive") }
Switch Statement in R:
Implement switch statements for multiple conditions.
# Switch statement in R day <- "Monday" message <- switch(day, "Monday" = "Start of the week", "Friday" = "End of the week", "Default message") print(message)
For Loop in R:
Use for loops for iterative tasks.
# For loop in R for (i in 1:5) { print(paste("Iteration", i)) }
While Loop in R:
Implement while loops for repetitive tasks.
# While loop in R i <- 1 while (i <= 5) { print(paste("Iteration", i)) i <- i + 1 }
Repeat and Break Statements in R:
Utilize repeat and break statements for indefinite loops.
# Repeat and break statements in R i <- 1 repeat { print(paste("Iteration", i)) i <- i + 1 if (i > 5) break }
Conditional Execution in R Programming:
Execute code conditionally based on logical conditions.
# Conditional execution in R programming x <- 10 if (x > 0) { print("Positive") }
Nested Control Statements in R:
Nest control statements for complex logic.
# Nested control statements in R for (i in 1:3) { for (j in 1:3) { print(paste("Nested Iteration", i, j)) } }
Control Flow in R:
Manage control flow using a combination of statements.
# Control flow in R x <- 10 if (x > 0) { print("Positive") } else { print("Non-positive") }
Error Handling Within Control Statements in R:
Handle errors within control statements using tryCatch().
# Error handling within control statements in R tryCatch({ x <- 1 / 0 print(x) }, error = function(e) { print(paste("Error:", e$message)) })