How do you check if value_1 is greater than 26?
value_1 > 26
Flip the card by pressing the q key.
How do you check if value_2 is equal to "some character data"?
value_2 == "some character data"
Use == (two equals signs) to check equality. A single = is used for assignment.
How do you check if value_1 is one of the values 1, 2, or 3?
Use %in%
value_1 %in% c(1, 2, 3)
How do you check if value_1 is NA?
is.na(value_1)
Returns TRUE if the value is NA and FALSE otherwise.
What does ! do to a condition?
It reverses the condition, so TRUE becomes FALSE and FALSE becomes TRUE.
How do you check if value_1 is not in c(1, 2, 3)?
Enclose the condition in !()
!(value_1 %in% c(1, 2, 3))
How do you check if value_1 is greater than 26 and less than 30?
value_1 > 26 & value_1 < 30
& returns TRUE only if both conditions are TRUE.
How do you check if value_1 is greater than 26 or value_2 is equal to "some character data"?
value_1 > 26 | value_2 == "some character data"
| returns TRUE if at least one condition is TRUE.
What does this return?
c(1, 2, 3, 4, 5) > 2
[1] FALSE FALSE TRUE TRUE TRUE
How do you set result to 42 only if value_1 is "A"?
if (value_1 == "A") {
result <- 42
}
How do you set result to 42 if value_1 is "A" and to 26 if value_1 is "B"?
Use else if to check additional conditions
if (value_1 == "A") {
result <- 42
} else if (value_1 == "B") {
result <- 26
}
if (value_1 == "A") {
result <- 42
} else if (value_1 == "B") {
result <- 26
}
How do you set result to NA if value_1 is neither "A" nor "B"?
Add an else to run code if none of the conditions are TRUE
if (value_1 == "A") {
result <- 42
} else if (value_1 == "B") {
result <- 26
} else {
result <- NA
}
Why doesn’t this work?
value_1 <- c("A", "B", "C")
if (value_1 == "A") {
result <- 42
}
if statements need a single TRUE or FALSE value.
value_1 == "A" returns a vector of three values (TRUE FALSE FALSE), so R throws an error.
Can you use a function that returns TRUE or FALSE as the condition in an if statement?
Yes. A function that returns TRUE or FALSE, like str_detect() from stringr, works as a condition
library(stringr)
if (str_detect(value_1, "A")) {
result <- 42
}
How do you use if statements inside of functions?
Put the if statement inside the function and return() the result after it
function_name <- function(input_1, input_2){
if (input_2 == "A") {
output_value <- input_1 * 42
} else {
output_value <- NA
}
return(output_value)
}
function_name <- function(input_1, input_2){
if (input_2 == "A") {
output_value <- input_1 * 42
} else if (input_2 == "B") {
output_value <- input_1 * 26
} else {
output_value <- NA
}
return(output_value)
}
What does function_name(2, "B") return?
[1] 52
input_2 is not "A", so R checks the else if. input_2 is "B", so output_value is 2 * 26.
function_name <- function(input_1, input_2){
if (input_2 == "A") {
output_value <- input_1 * 42
} else if (input_2 == "B") {
output_value <- input_1 * 26
} else {
output_value <- NA
}
return(output_value)
}
What does function_name(2, "C") return?
[1] NA
None of the conditions are TRUE, so the code in the else block runs.