📖 Scenario: You are working with a list of daily temperatures recorded in a city. You want to find which days were warm, meaning the temperature was above 20 degrees Celsius.
🎯 Goal: Build a program that uses logical indexing to select only the warm days from the temperature data.
📋 What You'll Learn
Create a numeric vector called temps with the exact values: 18, 22, 19, 25, 21, 17, 23
Create a logical vector called warm_days that is TRUE where temps is greater than 20
Use logical indexing with warm_days to create a vector called warm_temps containing only temperatures above 20
Print the warm_temps vector
💡 Why This Matters
🌍 Real World
Logical indexing is useful when you want to filter data based on conditions, like selecting only warm days from weather data.
💼 Career
Data analysts and scientists often use logical indexing to quickly extract relevant data for reports and decision making.
Progress0 / 4 steps
1
Create the temperature data vector
Create a numeric vector called temps with these exact values: 18, 22, 19, 25, 21, 17, 23
R Programming
Hint
Use the c() function to combine numbers into a vector.
2
Create a logical vector for warm days
Create a logical vector called warm_days that is TRUE where temps is greater than 20
R Programming
Hint
Use the comparison operator > to create a logical vector.
3
Select warm temperatures using logical indexing
Use logical indexing with warm_days to create a vector called warm_temps containing only temperatures above 20
R Programming
Hint
Use square brackets [] with the logical vector to select elements.
4
Print the warm temperatures
Write a print statement to display the warm_temps vector
R Programming
Hint
Use the print() function to show the vector.
Practice
(1/5)
1. What does logical indexing in R do when applied to a vector like v[c(TRUE, FALSE, TRUE)]?
easy
A. Selects elements of v where the index is TRUE
B. Selects elements of v where the index is FALSE
C. Returns the length of v
D. Returns an error because logical vectors cannot index
Solution
Step 1: Understand logical indexing
Logical indexing uses a TRUE/FALSE vector to pick elements from the original vector where TRUE is present.
Step 2: Apply logical vector to v
Elements at positions with TRUE in the logical vector are selected; others are skipped.
Final Answer:
Selects elements of v where the index is TRUE -> Option A
Quick Check:
TRUE picks elements [OK]
Hint: TRUE means pick element, FALSE means skip [OK]
Common Mistakes:
Thinking FALSE picks elements
Confusing logical indexing with numeric indexing
Assuming logical vector length doesn't matter
2. Which of the following is the correct syntax to select elements of vector x where values are greater than 5 using logical indexing?
easy
A. x[> 5]
B. x > 5[x]
C. x[x > 5]
D. x[5 > x]
Solution
Step 1: Understand logical condition syntax
The condition x > 5 creates a logical vector TRUE where elements are greater than 5.
Step 2: Use logical vector to index x
Using x[x > 5] selects elements of x where the condition is TRUE.
Final Answer:
x[x > 5] -> Option C
Quick Check:
Correct logical indexing syntax [OK]
Hint: Put condition inside square brackets after vector [OK]
Common Mistakes:
Placing condition outside brackets
Using invalid syntax like x[> 5]
Swapping operands in condition
3. What is the output of the following R code?
v <- c(10, 3, 7, 2, 9)
v[v >= 7]
medium
A. [1] 10 3 7 2 9
B. [1] 3 2
C. Error: invalid logical indexing
D. [1] 10 7 9
Solution
Step 1: Evaluate the logical condition v >= 7
This creates a logical vector: TRUE, FALSE, TRUE, FALSE, TRUE for elements 10, 3, 7, 2, 9 respectively.
Step 2: Select elements where condition is TRUE
Elements at TRUE positions are 10, 7, and 9.
Final Answer:
[1] 10 7 9 -> Option D
Quick Check:
Only values >= 7 selected [OK]
Hint: Check which elements satisfy condition, pick those [OK]