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 # TRUEThis 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 # TRUEHere's how NOT transforms logical values:
| A | !A |
|---|---|
| TRUE | FALSE |
| FALSE | TRUE |
Challenge
EasyApply 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 <- FALSEUse the NOT operator along with AND and OR operators to create the following variables:
- Create a variable called
stay_drythat stores the opposite ofis_raining - Create a variable called
will_get_wetthat storesTRUEif it is raining AND the person does NOT have an umbrella - Create a variable called
can_relaxthat storesTRUEif it is the weekend AND the person does NOT have homework - Create a variable called
should_go_outthat storesTRUEif it is NOT raining OR the person has an umbrella - Create a variable called
productive_daythat storesTRUEif 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 # TRUEUse 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 |
|---|---|
| TRUE | FALSE |
| FALSE | TRUE |
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)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