Conditionals (flash cards)

Question 1

How do you check if value_1 is greater than 26?

value_1 > 26

Question 2

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.

Question 3

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)

Question 4

How do you check if value_1 is NA?

is.na(value_1)

Returns TRUE if the value is NA and FALSE otherwise.

Question 5

What does ! do to a condition?

It reverses the condition, so TRUE becomes FALSE and FALSE becomes TRUE.

Question 6

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))

Question 7

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.

Question 8

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.

Question 9

What does this return?

c(1, 2, 3, 4, 5) > 2
[1] FALSE FALSE  TRUE  TRUE  TRUE

Question 10

How do you set result to 42 only if value_1 is "A"?

if (value_1 == "A") {
  result <- 42
}

Question 11

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
}

Question 12

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
}

Question 13

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.

Question 14

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
}

Question 15

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)
}

Question 16

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.

Question 17

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.