Conditional statements allow R programs to execute different blocks of code based on certain conditions.
2024
Conditional statements allow R programs to execute different blocks of code based on certain conditions.
if Statementif (condition) {
# code to execute if condition is True
}
if Statementx <- 10
if (x > 5) {
print("x is greater than 5")
}
## [1] "x is greater than 5"
x > 5) is True.else Statementif with elseif (condition) {
# code if condition is True
} else {
# code if condition is False
}
else Statementx <- 3
if (x > 5) {
print("x is greater than 5")
} else {
print("x is not greater than 5")
}
## [1] "x is not greater than 5"
False.else if Statementif (condition1) {
# code if condition1 is True
} else if (condition2) {
# code if condition2 is True
} else {
# code if neither condition is True
}
else if Statementx <- 5
if (x > 5) {
print("x is greater than 5")
} else if (x == 5) {
print("x is equal to 5")
} else {
print("x is less than 5")
}
## [1] "x is equal to 5"
else if block.any Function in RThe any function is a logical operation in R that tests if any of the elements in a given vector or data structure are TRUE.
values <- c(TRUE, FALSE, FALSE) result <- any(values) print(result) # Output: TRUE
## [1] TRUE
all Function in RThe all function is a logical operation in R that tests if all of the elements in a given vector or data structure are TRUE.
values <- c(TRUE, FALSE, FALSE) result <- all(values) print(result) # Output: FALSE
## [1] FALSE
ifelse Functionifelse(test_expression, yes, no)
The ifelse function is a vectorized conditional function which evaluates each element of the test_expression.
If the condition for an element is TRUE, it returns the corresponding element from yes; otherwise, it returns the corresponding element from no.
ifelse Functionx <- 5 result <- ifelse(x > 10, "Greater than 10", "Less or equal to 10") print(result)
## [1] "Less or equal to 10"
"Less or equal to 10".ifelse can operate on vectorsx <- c(5, 10, 15) result <- ifelse(x > 10, "Greater than 10", "Less or equal to 10") print(result)
## [1] "Less or equal to 10" "Less or equal to 10" "Greater than 10"
["Less or equal to 10", "Less or equal to 10", "Greater than 10"].ifelseyes and no arguments must be the same type, or they will be coerced to a common type, which might not be intended.x <- c(5, 10, 15) result <- ifelse(x > 10, x, "Less or equal to 10") print(result)
## [1] "Less or equal to 10" "Less or equal to 10" "15"
Returns a character vector: ["Less or equal to 10", "Less or equal to 10", "15"].
Last element is coerced to character.
Loops in R are used to execute a block of code repeatedly.
for Loopfor (element in sequence) {
# code to execute for each element
}
for Loopfruits <- c("apple", "banana", "cherry")
for (fruit in fruits) {
print(fruit)
}
## [1] "apple" ## [1] "banana" ## [1] "cherry"
for Loop# Print numbers 1 through 5
for(i in 1:5) {
print(i)
}
## [1] 1 ## [1] 2 ## [1] 3 ## [1] 4 ## [1] 5
while Loopwhile (condition) {
# code to execute while condition is True
}
while Loop# Print numbers 1 through 5
i <- 1
while(i <= 5) {
print(i)
i <- i + 1
}
## [1] 1 ## [1] 2 ## [1] 3 ## [1] 4 ## [1] 5
TRUE.What does the following code print?
if (FALSE) {
print("False!")
} else if (TRUE) {
print("Now True!")
} else {
print("Finally True!")
}
else if block is executed because its condition is True.How many times does the following loop run?
x <- 3
while (x < 8) {
print("Looping!")
x <- x + 2
}
x by 2 each time, starting from 3 and stopping before it reaches 8.Consider the following code:
numbers <- c(1, 4, 7, 10) # Initial vector of numbers
target <- 5 # Target value
i <- 1 # Index to traverse the vector
while(any(numbers != target)) {
i <- ifelse(numbers[i]==target, i + 1, i)
numbers[i] <- ifelse(numbers[i] < target,
numbers[i] + 1,
numbers[i] - 1)
print(numbers)
}
What will be the final state of the numbers vector after the loop terminates?
c(5, 5, 5, 5)c(2, 5, 8, 11)c(1, 4, 7, 10)ifelse statement to adjust each element towards the target value by either incrementing or decrementing. The loop iterates to the next element only when numbers[i] has reached the target value. Thus ensuring the loop will terminate when all numbers become equal to the target value of 5.What will the following code output?
for(i in c(1, 3, 5)) {
if(i %% 2 == 0) {
print("Even")
} else {
print("Odd")
}
}
What will the following code output?
for(i in c(1, 3, 5)) {
if(i %% 2 == 0) {
print("Even")
} else {
print("Odd")
}
}