[1] "underage" "adult" "underage" "underage"
University of Münster
2025-10-30
if-else control structures are very helpful for solving complex conditional operations:
if (condition) expression
example:
The expression can take multiple lines which are enclosed with braces:
Sometimes, the if structure is extended with else:
if (condition) expression1 else expression2
example:
The if structure is not vectorized.
That is, the condition must return a single logical TRUE or FALSE value. This code will throw an error or warning message:
ifelse is a vectorized version of if() else which returns a vector:
ifelse(condition, expression_true, expression_false)
Example:
What is the object results after executing the code?
age <- c(15, 19, 21, 18, 17, 23)
cut_full_aged <- 18
language <- "german"
if (language == "english") {
label_adult <- "adult"
label_underage <- "underage"
} else if (language == "german") {
label_adult <- "Volljährig"
label_underage <- "Minderjährig"
} else {
stop("Unknown language!")
}
results <- ifelse(age < cut_full_aged, label_underage, label_adult)age <- c(15, 19, 21, 18, 17, 23)
cut_full_aged <- 18
language <- "german"
if (language == "english") {
label_adult <- "adult"
label_underage <- "underage"
} else if (language == "german") {
label_adult <- "Volljährig"
label_underage <- "Minderjährig"
} else {
stop("Unknown language!")
}
results <- ifelse(age < cut_full_aged, label_underage, label_adult)
results[1] "Minderjährig" "Volljährig" "Volljährig" "Volljährig" "Minderjährig"
[6] "Volljährig"
What is contained in res after executing the code?
With a for loop you can iterate the values of a vector:
for (variable in vector) expression
example:
An expression can have multiple lines enclosed in braces:
Oftentimes for loops are used to iterate multiple vectors.
Can you read this code and predict the object comparison ?
You can use for loops to iterate through a dataframe:
mtcars$impact <- "average"
for(i in 1:nrow(mtcars)) {
if (mtcars$mpg[i] < 18 && mtcars$cyl[i] == 8 && mtcars$wt[i] > 4) {
mtcars$impact[i] <- "Big waster!"
}
if (mtcars$mpg[i] > 22 && mtcars$cyl[i] == 4 && mtcars$wt[i] < 2.59) {
mtcars$impact[i] <- "Light eco!"
}
}
mtcars[, c("mpg", "cyl", "wt", "impact")]mtcars$impact <- "average"
for(i in 1:nrow(mtcars)) {
if (mtcars$mpg[i] < 18 && mtcars$cyl[i] == 8 && mtcars$wt[i] > 4)
mtcars$impact[i] <- "Big waster!"
if (mtcars$mpg[i] > 22 && mtcars$cyl[i] == 4 && mtcars$wt[i] < 2.59)
mtcars$impact[i] <- "Light eco!"
}
mtcars[1:10 , c("mpg", "cyl", "wt", "impact")]Repeated operations can be put into functions.
functionname <- function(argument, argument, argument, …) {expression}
return() stops the function and returns an object. If no return is defined, the function will return the last object that is printed.
w20_die <- function(dice) {
if (dice < 1 || dice > 20) return("This is not a decent W20 die!")
if (dice == 20) return("Critical hit!!")
if (dice == 1) return("Epic fail")
dice
}
w20_die(20)
[1] "Critical hit!!"
w20_die(3)
[1] 3
w20_die(1)
[1] "Epic fail"
w20_die(0)
[1] "This is not a decent W20 die!"What happens here?
w20_die <- function(dice) {
if (dice < 1 || dice > 20) return("This is not a decent W20 die!")
if (dice == 20) return("Critical hit!!")
if (dice == 1) return("Epic fail")
dice
}w20_die function so it will return a vector with results when you provide a vector with numbers as an argument:w20_die <- function(dice) {
out <- dice
for(i in 1:length(dice)) {
if (dice[i] < 1 || dice[i] > 20) out[i] <- "This is not a decent W20 die!"
if (dice[i] == 20) out[i] <- "Critical hit!!"
if (dice[i] == 1) out[i] <- "Epic fail"
}
out
}
results <- c(21, 2, 16, 20, 1, 0)
w20_die(results)[1] "This is not a decent W20 die!" "2"
[3] "16" "Critical hit!!"
[5] "Epic fail" "This is not a decent W20 die!"
w20_die <- function(dice) {
out <- dice
out[dice < 1 | dice > 20] <- "This is not a decent W20 die!"
out[dice == 20] <- "Critical hit!!"
out[dice == 1] <- "Epic fail"
out
}
results <- c(21, 2, 16, 20, 1, 0)
w20_die(results)[1] "This is not a decent W20 die!" "2"
[3] "16" "Critical hit!!"
[5] "Epic fail" "This is not a decent W20 die!"
w20_die <- function(dice) {
ifelse(dice < 1 | dice > 20, "This is not a decent W20 die!",
ifelse(dice == 20, "Critical hit!!",
ifelse(dice == 1, "Epic fail", dice)))
}
results <- c(21, 2, 16, 20, 1, 0)
w20_die(results)[1] "This is not a decent W20 die!" "2"
[3] "16" "Critical hit!!"
[5] "Epic fail" "This is not a decent W20 die!"
Jürgen Wilbert - Introduction to R