evoalgs-r-practise/if_else.R
2024-04-30 10:03:37 +02:00

27 lines
547 B
R

# Simple if-else statement
x <- 5
if (x > 0) {
print("x is positive")
} else {
print("x is non-positive")
}
# if-else-if ladder
score <- 85
if (score >= 90) {
print("Grade: A")
} else if (score >= 80) {
print("Grade: B")
} else if (score >= 70) {
print("Grade: C")
} else {
print("Grade: F")
}
# Vectorized ifelse statement
scores <- c(92, 81, 58, 77, 85)
grades <- ifelse(scores >= 90, "A",
ifelse(scores >= 80, "B",
ifelse(scores >= 70, "C", "F")))
print("Vectorized Grades:")
print(grades)