0
0
R Programmingprogramming~20 mins

If-else statements in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If-Else Mastery in R
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested if-else in R
What is the output of this R code?
R Programming
x <- 7
if (x > 10) {
  print("Greater than 10")
} else if (x > 5) {
  print("Between 6 and 10")
} else {
  print("5 or less")
}
A[1] "Between 6 and 10"
B[1] "Greater than 10"
C[1] "5 or less"
DError in if (x > 10) { : missing value where TRUE/FALSE needed
Attempts:
2 left
💡 Hint
Check the conditions in order and see which one matches the value of x.
Predict Output
intermediate
2:00remaining
Output of if-else with vector condition
What happens when you run this R code?
R Programming
x <- c(3, 7, 12)
if (x > 5) {
  print("Some values are greater than 5")
} else {
  print("No values greater than 5")
}
AWarning: the condition has length > 1 and only the first element will be used
B[1] "No values greater than 5"
C[1] "Some values are greater than 5"
DNULL
Attempts:
2 left
💡 Hint
Remember that if expects a single TRUE or FALSE, not a vector.
🔧 Debug
advanced
2:00remaining
Identify the error in this if-else code
What error does this R code produce?
R Programming
x <- 4
if x > 3 {
  print("x is greater than 3")
} else {
  print("x is 3 or less")
}
AError: object 'x' not found
BError in if (x > 3) { : missing value where TRUE/FALSE needed
CSyntaxError: unexpected symbol in "if x"
DNo error, prints "x is greater than 3"
Attempts:
2 left
💡 Hint
Check the syntax of the if statement in R.
🧠 Conceptual
advanced
2:00remaining
Behavior of if-else with NA condition
What is the output of this R code?
R Programming
x <- NA
if (x) {
  print("True")
} else {
  print("False")
}
A[1] "True"
BNULL
C[1] "False"
DError in if (x) { : missing value where TRUE/FALSE needed
Attempts:
2 left
💡 Hint
Think about how R treats NA in logical conditions.
Predict Output
expert
3:00remaining
Output of complex nested if-else with multiple variables
What is the output of this R code?
R Programming
a <- 5
b <- 10
if (a > 3) {
  if (b < 5) {
    print("a > 3 and b < 5")
  } else if (b < 15) {
    print("a > 3 and b between 5 and 15")
  } else {
    print("a > 3 and b >= 15")
  }
} else {
  print("a <= 3")
}
A[1] "a > 3 and b < 5"
B[1] "a > 3 and b between 5 and 15"
C[1] "a > 3 and b >= 15"
D[1] "a <= 3"
Attempts:
2 left
💡 Hint
Check the values of a and b and follow the nested conditions carefully.