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
The if
statement in R allows you to conditionally execute a block of code. It's a foundational element of any programming language, allowing for decision-making in your scripts or functions.
In this tutorial, we'll explore:
if
Statementif
StatementThe basic structure of an if
statement is:
if (condition) { # code to execute if condition is TRUE }
The condition inside the parentheses is evaluated, and if it's TRUE
, the code block within the curly braces {}
will be executed.
x <- 10 if (x > 5) { print("x is greater than 5") }
When you run this code, it will output: x is greater than 5
You can combine multiple conditions using logical operators:
&
(and): Both conditions have to be TRUE
|
(or): At least one condition has to be TRUE
!
(not): Reverses the truth value of the conditionx <- 7 y <- 15 if (x > 5 & y < 20) { print("Both conditions are true!") } if (x < 5 | y < 20) { print("At least one condition is true!") }
This will output:
[1] "Both conditions are true!" [1] "At least one condition is true!"
Ensure your condition returns a single logical value: If the condition in the if
statement returns a vector with more than one value, R will warn you and only consider the first element.
vec <- c(TRUE, FALSE) if (vec) { print("This will give a warning!") }
You'll get: Warning: the condition has length > 1 and only the first element will be used
Comparing for exact equality with floating-point numbers: Due to the way floating-point arithmetic works, it's often not recommended to test for exact equality (==
) with floating-point numbers. Instead, you can use a tolerance approach or the all.equal()
function.
x <- sqrt(2)^2 if (!all.equal(x, 2)) { print("This won't print, because x is essentially 2!") }
The if
statement in R allows for decision-making based on conditions, enabling you to create dynamic scripts that respond to varying inputs or situations. It's essential to remember that the condition in an if
statement should ideally evaluate to a single logical value. Using combined conditions and being aware of common pitfalls ensures effective and error-free conditional programming in R.
R code examples for using if statements:
# Simple if statement x <- 10 if (x > 5) { print("x is greater than 5") }
Comparing values with if statement in R:
# Comparing values with if statement a <- 7 b <- 5 if (a > b) { print("a is greater than b") }
Logical operators in if statements in R:
# Using logical operators in if statement age <- 25 if (age >= 18 & age <= 60) { print("Person is between 18 and 60 years old") }
Using if statements for control flow in R:
# Using if statements for control flow temperature <- 28 if (temperature > 30) { print("It's a hot day!") } else if (temperature > 20) { print("It's a pleasant day.") } else { print("It's a cold day.") }
Vectorized if statement in R programming:
# Vectorized if statement using ifelse() vector_x <- c(2, 7, 10, 3, 8) result_vector <- ifelse(vector_x > 5, "Greater than 5", "Less than or equal to 5")
Nested if statements in R:
# Nested if statements x <- 10 y <- 8 if (x > 5) { if (y > 5) { print("Both x and y are greater than 5") } else { print("x is greater than 5, but y is not greater than 5") } } else { print("x is not greater than 5") }