Menu
Coddy logo textTech

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 FALSE

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

The 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 icon

Challenge

Easy

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

  1. Create a vector called comfortable_temp that stores TRUE for each temperature that is greater than or equal to 20 AND less than or equal to 30
  2. Create a vector called high_humidity that stores TRUE for each humidity value that is greater than 70
  3. Create a vector called pleasant_day that stores TRUE for each day where the temperature is between 20 and 30 (inclusive) AND humidity is less than or equal to 60
  4. Create a vector called extreme_weather that stores TRUE for each day where the temperature is greater than 30 OR the humidity is greater than 80
  5. Create a vector called stay_inside that stores TRUE for 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 FALSE

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

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

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

All lessons in Fundamentals