Vectorized Logic Part 2
Part of the Fundamentals section of Coddy's R journey — lesson 21 of 78.
Just like AND and OR have vectorized versions, the NOT operator also works element-wise on vectors. When you apply ! to a vector of logical values, it flips each element individually.
passed <- c(TRUE, FALSE, TRUE, FALSE)
!passed # FALSE TRUE FALSE TRUEEach TRUE becomes FALSE, and each FALSE becomes TRUE. This is particularly useful when you want to find elements that don't meet a certain condition:
temperatures <- c(18, 25, 30, 15, 22)
# Find temperatures that are NOT above 20
!(temperatures > 20) # TRUE FALSE FALSE TRUE FALSEYou can combine the vectorized NOT with & and | to build complex filters:
ages <- c(16, 25, 18, 30, 14)
has_permission <- c(TRUE, TRUE, FALSE, TRUE, TRUE)
# Can participate if: age >= 18 OR (under 18 AND has permission)
can_participate <- ages >= 18 | (ages < 18 & has_permission)
# TRUE TRUE FALSE TRUE TRUERemember: use single &, |, and ! when working with vectors, and double &&, || when comparing single values.
Challenge
EasyUse the vectorized NOT operator (!) along with & and | to create complex element-wise logical conditions.
You are provided with the following vectors:
scores <- c(45, 72, 88, 55, 91, 38, 67)
attended <- c(TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE)
submitted_homework <- c(TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE)Use vectorized logical operators including the NOT operator to create the following logical vectors:
- Create a vector called
failedthat storesTRUEfor each score that is NOT greater than or equal to 60 - Create a vector called
absentthat stores the opposite ofattended(flip each value) - Create a vector called
at_riskthat storesTRUEfor each student who did NOT attend OR did NOT submit homework - Create a vector called
passing_but_absentthat storesTRUEfor each student who has a score greater than or equal to 60 AND did NOT attend - Create a vector called
needs_supportthat storesTRUEfor each student where: (score is less than 50) OR (did NOT attend AND did NOT submit homework)
Use the print() function to display failed, absent, at_risk, passing_but_absent, and needs_support in that exact order.
Cheat sheet
The NOT operator (!) works element-wise on vectors, flipping each logical value individually:
passed <- c(TRUE, FALSE, TRUE, FALSE)
!passed # FALSE TRUE FALSE TRUEUse ! to find elements that don't meet a condition:
temperatures <- c(18, 25, 30, 15, 22)
!(temperatures > 20) # TRUE FALSE FALSE TRUE FALSECombine vectorized NOT with & and | for complex filters:
ages <- c(16, 25, 18, 30, 14)
has_permission <- c(TRUE, TRUE, FALSE, TRUE, TRUE)
# Can participate if: age >= 18 OR (under 18 AND has permission)
can_participate <- ages >= 18 | (ages < 18 & has_permission)
# TRUE TRUE FALSE TRUE TRUERemember: use single &, |, and ! for vectors; use double &&, || for single values.
Try it yourself
# Given vectors
scores <- c(45, 72, 88, 55, 91, 38, 67)
attended <- c(TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE)
submitted_homework <- c(TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE)
# TODO: Write your code below
# 1. Create 'failed' - TRUE for scores NOT >= 60
# 2. Create 'absent' - opposite of attended
# 3. Create 'at_risk' - NOT attended OR NOT submitted homework
# 4. Create 'passing_but_absent' - score >= 60 AND NOT attended
# 5. Create 'needs_support' - (score < 50) OR (NOT attended AND NOT submitted homework)
# Print the results
print(failed)
print(absent)
print(at_risk)
print(passing_but_absent)
print(needs_support)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values