Menu
Coddy logo textTech

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 TRUE

Each 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 FALSE

You 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 TRUE

Remember: use single &, |, and ! when working with vectors, and double &&, || when comparing single values.

challenge icon

Challenge

Easy

Use 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:

  1. Create a vector called failed that stores TRUE for each score that is NOT greater than or equal to 60
  2. Create a vector called absent that stores the opposite of attended (flip each value)
  3. Create a vector called at_risk that stores TRUE for each student who did NOT attend OR did NOT submit homework
  4. Create a vector called passing_but_absent that stores TRUE for each student who has a score greater than or equal to 60 AND did NOT attend
  5. Create a vector called needs_support that stores TRUE for 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 TRUE

Use ! to find elements that don't meet a condition:

temperatures <- c(18, 25, 30, 15, 22)
!(temperatures > 20)  # TRUE FALSE FALSE TRUE FALSE

Combine 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 TRUE

Remember: 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)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals