Vectorized Logic Part 1
Part of the Fundamentals section of Coddy's R journey — lesson 20 of 78.
R has two versions of AND and OR operators. You've learned about && and ||, which work with single values. Now let's explore their vectorized counterparts: & and | (single symbols).
The key difference is that & and | compare values element by element when working with multiple values:
a <- c(TRUE, TRUE, FALSE, FALSE)
b <- c(TRUE, FALSE, TRUE, FALSE)
a & b # TRUE FALSE FALSE FALSE
a | b # TRUE TRUE TRUE FALSEEach position is compared independently. In the first position, both a and b are TRUE, so a & b returns TRUE. In the second position, a is TRUE but b is FALSE, so a & b returns FALSE.
This becomes powerful when combined with comparison operators:
scores <- c(85, 42, 91, 67, 55)
# Check which scores are between 50 and 80
scores >= 50 & scores <= 80 # FALSE FALSE FALSE TRUE TRUEThe result is a vector of logical values showing which elements meet both conditions. This element-wise behavior is essential for filtering and analyzing data in R.
Challenge
EasyUse the vectorized AND (&) and OR (|) operators to perform element-wise comparisons on vectors.
You are provided with the following vectors:
temperatures <- c(18, 25, 32, 15, 28, 35, 22)
humidity <- c(45, 80, 60, 30, 75, 85, 50)Use vectorized logical operators to create the following logical vectors:
- Create a vector called
comfortable_tempthat storesTRUEfor each temperature that is greater than or equal to 20 AND less than or equal to 30 - Create a vector called
high_humiditythat storesTRUEfor each humidity value that is greater than 70 - Create a vector called
pleasant_daythat storesTRUEfor each day where the temperature is between 20 and 30 (inclusive) AND humidity is less than or equal to 60 - Create a vector called
extreme_weatherthat storesTRUEfor each day where the temperature is greater than 30 OR the humidity is greater than 80 - Create a vector called
stay_insidethat storesTRUEfor each day where the temperature is less than 18 OR (the temperature is greater than 30 AND humidity is greater than 70)
Use the print() function to display comfortable_temp, high_humidity, pleasant_day, extreme_weather, and stay_inside in that exact order.
Cheat sheet
R has two versions of AND and OR operators:
&&and||work with single values&and|are vectorized operators that compare values element by element
The vectorized operators & and | compare each position independently:
a <- c(TRUE, TRUE, FALSE, FALSE)
b <- c(TRUE, FALSE, TRUE, FALSE)
a & b # TRUE FALSE FALSE FALSE
a | b # TRUE TRUE TRUE FALSEVectorized operators are powerful when combined with comparison operators:
scores <- c(85, 42, 91, 67, 55)
# Check which scores are between 50 and 80
scores >= 50 & scores <= 80 # FALSE FALSE FALSE TRUE TRUEThe result is a vector of logical values showing which elements meet the conditions.
Try it yourself
# Provided vectors
temperatures <- c(18, 25, 32, 15, 28, 35, 22)
humidity <- c(45, 80, 60, 30, 75, 85, 50)
# TODO: Write your code below
# 1. Create comfortable_temp: TRUE when temp >= 20 AND temp <= 30
# 2. Create high_humidity: TRUE when humidity > 70
# 3. Create pleasant_day: TRUE when temp is 20-30 AND humidity <= 60
# 4. Create extreme_weather: TRUE when temp > 30 OR humidity > 80
# 5. Create stay_inside: TRUE when temp < 18 OR (temp > 30 AND humidity > 70)
# Print the results
print(comfortable_temp)
print(high_humidity)
print(pleasant_day)
print(extreme_weather)
print(stay_inside)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