Challenge - 5 Problems
Logical Mastery in R
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of logical operations in R
What is the output of the following R code?
R Programming
x <- c(TRUE, FALSE, TRUE)
y <- c(FALSE, FALSE, TRUE)
result <- x & y
print(result)Attempts:
2 left
💡 Hint
Remember that '&' is element-wise AND in R.
✗ Incorrect
The '&' operator compares each element of x and y and returns TRUE only if both are TRUE.
Here, the pairs are (TRUE, FALSE), (FALSE, FALSE), (TRUE, TRUE), so the results are FALSE, FALSE, TRUE.
❓ Predict Output
intermediate2:00remaining
Result of logical negation in R
What will be printed by this R code?
R Programming
flag <- c(TRUE, FALSE, FALSE, TRUE)
neg_flag <- !flag
print(neg_flag)Attempts:
2 left
💡 Hint
The '!' operator flips TRUE to FALSE and vice versa.
✗ Incorrect
The '!' operator negates each element in the logical vector.
So TRUE becomes FALSE, and FALSE becomes TRUE.
❓ Predict Output
advanced2:00remaining
Logical vector recycling in R
What is the output of this R code snippet?
R Programming
a <- c(TRUE, FALSE)
b <- c(TRUE, TRUE, FALSE, FALSE)
result <- a | b
print(result)Attempts:
2 left
💡 Hint
Remember R recycles shorter vectors to match the length of longer vectors.
✗ Incorrect
The vector a is recycled to length 4: TRUE, FALSE, TRUE, FALSE.
Then element-wise OR with b gives TRUE, TRUE, TRUE, FALSE.
❓ Predict Output
advanced2:00remaining
Logical indexing with boolean vectors in R
What will this R code print?
R Programming
values <- c(10, 20, 30, 40, 50) filter <- c(TRUE, FALSE, TRUE, FALSE, TRUE) selected <- values[filter] print(selected)
Attempts:
2 left
💡 Hint
Logical vectors can be used to pick elements from another vector.
✗ Incorrect
The logical vector filter selects elements where it is TRUE.
So elements 1, 3, and 5 from values are selected.
🧠 Conceptual
expert2:00remaining
Understanding the difference between '&' and '&&' in R
Which statement best describes the difference between '&' and '&&' in R?
Attempts:
2 left
💡 Hint
Think about how R treats vectorized logical operations versus single comparisons.
✗ Incorrect
The single ampersand '&' compares each element of vectors and returns a vector of results.
The double ampersand '&&' only compares the first element of each vector and returns a single TRUE or FALSE.