📖 Scenario: You are working with a small dataset of students' test scores. You want to find out which students passed based on certain conditions using logical operators.
🎯 Goal: Build a simple R program that uses logical operators &, |, and ! to check students' scores and print who passed.
📋 What You'll Learn
Create a named vector called scores with exact student names and scores
Create a variable called pass_mark with the value 60
Use logical operators &, |, and ! to find students who passed or failed
Print the final result showing which students passed
💡 Why This Matters
🌍 Real World
Logical operators help filter and select data based on conditions, useful in data analysis and decision making.
💼 Career
Understanding logical operators is essential for data scientists, analysts, and programmers to write conditions and control program flow.
Progress0 / 4 steps
1
Create the scores vector
Create a named numeric vector called scores with these exact entries: Alice = 58, Bob = 75, Charlie = 62, Diana = 45, Eva = 80.
R Programming
Hint
Use the c() function with named elements like Alice = 58.
2
Set the passing mark
Create a variable called pass_mark and set it to the number 60.
R Programming
Hint
Just assign the number 60 to pass_mark.
3
Use logical operators to find who passed
Create a logical vector called passed that is TRUE for students with scores greater than or equal to pass_mark and FALSE otherwise. Use the logical operator >=.
R Programming
Hint
Use passed <- scores >= pass_mark to get TRUE or FALSE for each student.
4
Print the students who passed
Use print() to display the names of students who passed. Use the logical vector passed to select names from scores.
R Programming
Hint
Use names(scores)[passed] inside print() to show who passed.
Practice
(1/5)
1. Which logical operator in R checks every element of a vector and returns a vector of results?
easy
A. ==
B. & and |
C. !
D. && and ||
Solution
Step 1: Understand vectorized logical operators
In R, & and | operate element-wise on vectors, returning a vector of TRUE/FALSE values for each element.
Step 2: Compare with single-element operators
&& and || only check the first element of the vectors and return a single TRUE/FALSE value.
Final Answer:
& and | -> Option B
Quick Check:
Element-wise operators = & and | [OK]
Hint: Use & and | for vectors, && and || for single values [OK]
Common Mistakes:
Confusing && and & as the same
Using || for vector comparisons
Thinking ! works on multiple elements like &
2. Which of the following is the correct syntax to negate a logical vector v in R?
easy
A. !v
B. not(v)
C. ~v
D. v != TRUE
Solution
Step 1: Identify the negation operator in R
The logical NOT operator in R is !, which negates each element of a logical vector.
Step 2: Check other options
not() is not a base R function, ~ is a formula operator, and v != TRUE works but is less direct and less clear.
v1 && v2 checks only first elements: TRUE && FALSE = FALSE Result: [FALSE]
Final Answer:
[1] FALSE FALSE TRUE
[1] FALSE -> Option D
Quick Check:
& compares all elements, && only first [OK]
Hint: & works element-wise; && checks first element only [OK]
Common Mistakes:
Thinking && works element-wise
Mixing up & and && results
Ignoring vector length in &&
4. Identify the error in this R code snippet:
v <- c(TRUE, FALSE, TRUE)
if (v & TRUE) {
print("Yes")
} else {
print("No")
}
medium
A. Using & inside if condition causes error due to vector length
B. Missing parentheses around v & TRUE
C. Should use | instead of &
D. No error, code runs fine
Solution
Step 1: Understand if condition requirements
The if statement expects a single TRUE or FALSE value, not a vector.
Step 2: Analyze v & TRUE
v & TRUE returns a logical vector of length 3, which causes an error in if because it cannot handle multiple values.
Final Answer:
Using & inside if condition causes error due to vector length -> Option A
Quick Check:
if needs single TRUE/FALSE, & returns vector [OK]
Hint: Use && in if conditions for single TRUE/FALSE [OK]
Common Mistakes:
Using & instead of && in if
Assuming if accepts vector
Ignoring error message about length
5. You have two logical vectors:
a <- c(TRUE, FALSE, TRUE, FALSE)
b <- c(FALSE, TRUE, TRUE, TRUE)
Write an expression using logical operators to find elements where either a is TRUE and b is FALSE, or a is FALSE and b is TRUE. What is the resulting vector?
hard
A. a | b resulting in [TRUE TRUE TRUE TRUE]
B. a & b resulting in [FALSE FALSE TRUE FALSE]
C. (a & !b) | (!a & b) resulting in [TRUE TRUE FALSE TRUE]
D. !(a | b) resulting in [FALSE FALSE FALSE FALSE]
Solution
Step 1: Understand the condition
We want TRUE where exactly one of a or b is TRUE, but not both. This is the XOR condition.
Step 2: Write expression and evaluate
(a & !b) | (!a & b) checks where a is TRUE and b is FALSE, or vice versa. Evaluate: a: TRUE FALSE TRUE FALSE b: FALSE TRUE TRUE TRUE a & !b: TRUE FALSE FALSE FALSE !a & b: FALSE TRUE FALSE TRUE OR: TRUE TRUE FALSE TRUE
Final Answer:
(a & !b) | (!a & b) resulting in [TRUE TRUE FALSE TRUE] -> Option C
Quick Check:
XOR = (a & !b) | (!a & b) [OK]
Hint: Use (a & !b) | (!a & b) for XOR logic [OK]
Common Mistakes:
Using & or | alone instead of XOR logic
Forgetting to negate one vector
Confusing XOR with OR
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.