Menu
Coddy logo textTech

Logical Operators Part 2 (NOT)

Part of the Fundamentals section of Coddy's R journey — lesson 18 of 78.

The NOT operator (!) flips a logical value to its opposite. It turns TRUE into FALSE and FALSE into TRUE.

is_raining <- TRUE
!is_raining  # FALSE

is_closed <- FALSE
!is_closed   # TRUE

This operator is useful when you want to check if something is not the case. For example, instead of checking if a user is logged in, you might need to check if they're not logged in:

logged_in <- FALSE
if (!logged_in) {
  # Show login prompt
}

You can combine NOT with AND and OR operators to create more complex conditions:

has_ticket <- TRUE
is_vip <- FALSE

# Can enter if they have a ticket AND are NOT a VIP (regular entrance)
regular_entry <- has_ticket && !is_vip  # TRUE

Here's how NOT transforms logical values:

A!A
TRUEFALSE
FALSETRUE
challenge icon

Challenge

Easy

Apply the NOT operator (!) to flip logical values and combine it with other logical operators.

You are provided with the following variables:

is_raining <- TRUE
has_umbrella <- FALSE
is_weekend <- TRUE
has_homework <- TRUE
is_tired <- FALSE

Use the NOT operator along with AND and OR operators to create the following variables:

  1. Create a variable called stay_dry that stores the opposite of is_raining
  2. Create a variable called will_get_wet that stores TRUE if it is raining AND the person does NOT have an umbrella
  3. Create a variable called can_relax that stores TRUE if it is the weekend AND the person does NOT have homework
  4. Create a variable called should_go_out that stores TRUE if it is NOT raining OR the person has an umbrella
  5. Create a variable called productive_day that stores TRUE if the person is NOT tired AND it is NOT the weekend

Use the print() function to display stay_dry, will_get_wet, can_relax, should_go_out, and productive_day in that exact order.

Cheat sheet

The NOT operator (!) flips a logical value to its opposite:

is_raining <- TRUE
!is_raining  # FALSE

is_closed <- FALSE
!is_closed   # TRUE

Use NOT to check if something is not the case:

logged_in <- FALSE
if (!logged_in) {
  # Show login prompt
}

Combine NOT with AND (&&) and OR (||) operators:

has_ticket <- TRUE
is_vip <- FALSE

regular_entry <- has_ticket && !is_vip  # TRUE
A!A
TRUEFALSE
FALSETRUE

Try it yourself

# Given variables
is_raining <- TRUE
has_umbrella <- FALSE
is_weekend <- TRUE
has_homework <- TRUE
is_tired <- FALSE

# TODO: Write your code below
# Use the NOT operator (!) along with AND (&) and OR (|) operators
# to create the required variables

# 1. stay_dry - opposite of is_raining

# 2. will_get_wet - TRUE if raining AND NOT has umbrella

# 3. can_relax - TRUE if weekend AND NOT has homework

# 4. should_go_out - TRUE if NOT raining OR has umbrella

# 5. productive_day - TRUE if NOT tired AND NOT weekend

# Print the results in order
print(stay_dry)
print(will_get_wet)
print(can_relax)
print(should_go_out)
print(productive_day)
quiz iconTest yourself

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

All lessons in Fundamentals