📖 Scenario: You are working in a small shop and want to check which products are priced above a certain amount to decide which ones to promote.
🎯 Goal: Build a simple R program that uses comparison operators to find products priced above a threshold.
📋 What You'll Learn
Create a named numeric vector called products with exact product names and prices
Create a numeric variable called threshold with the value 50
Use a comparison operator to create a logical vector called expensive_products that is TRUE for products priced above threshold
Print the expensive_products vector
💡 Why This Matters
🌍 Real World
Shops often need to filter products by price to decide which items to promote or discount.
💼 Career
Understanding comparison operators is essential for data filtering and decision making in data analysis and programming jobs.
Progress0 / 4 steps
1
Create the products vector
Create a named numeric vector called products with these exact entries: apple = 30, banana = 20, cherry = 60, date = 80, elderberry = 40.
R Programming
Hint
Use the c() function with named elements like name = value.
2
Set the price threshold
Create a numeric variable called threshold and set it to 50.
R Programming
Hint
Just assign the number 50 to the variable threshold.
3
Find products priced above the threshold
Use the greater than operator > to create a logical vector called expensive_products that is TRUE for products priced above threshold.
R Programming
Hint
Use products > threshold to get TRUE or FALSE for each product.
4
Print the result
Print the expensive_products vector to see which products are above the threshold.
R Programming
Hint
Use print(expensive_products) to show the logical vector.
Practice
(1/5)
1. Which of the following comparison operators in R checks if two values are equal?
easy
A. !=
B. =
C. <>
D. ==
Solution
Step 1: Understand equality operator in R
In R, the operator to check if two values are equal is ==.
Step 2: Differentiate from other operators
= is for assignment, != means not equal, and <> is not valid in R.
Final Answer:
== -> Option D
Quick Check:
Equality operator = == [OK]
Hint: Use double equals (==) to test equality in R [OK]
Common Mistakes:
Using single = instead of ==
Confusing != with equality
Using <> which is invalid in R
2. Which of the following is the correct syntax to check if variable x is NOT equal to 10 in R?
easy
A. x != 10
B. x <> 10
C. x =! 10
D. x !== 10
Solution
Step 1: Identify the not equal operator in R
R uses != to check if two values are not equal.
Step 2: Check syntax correctness
<> and !== are not valid in R, and =! is incorrect syntax.
Final Answer:
x != 10 -> Option A
Quick Check:
Not equal operator = != [OK]
Hint: Use != to check inequality in R [OK]
Common Mistakes:
Using <> which is invalid in R
Writing =! instead of !=
Using !== which is from other languages
3. What is the output of the following R code?
x <- 5 y <- 7 x > y
medium
A. FALSE
B. TRUE
C. 5
D. Error
Solution
Step 1: Compare values of x and y
x is 5 and y is 7, so 5 > 7 is being checked.
Step 2: Evaluate the comparison
Since 5 is not greater than 7, the expression returns FALSE.
Final Answer:
FALSE -> Option A
Quick Check:
5 > 7 = FALSE [OK]
Hint: Compare numbers directly; 5 > 7 is false [OK]
Common Mistakes:
Confusing > with <
Expecting numeric output instead of logical
Thinking it causes an error
4. Identify the error in this R code snippet:
if (x = 10) { print("Yes") }
medium
A. No error, code is correct
B. Missing curly braces
C. Using = instead of ==
D. print() is incorrect function
Solution
Step 1: Check the condition syntax in if statement
In R, to compare values, use ==, not =.
Step 2: Understand the difference between assignment and comparison
= assigns a value, but == tests equality. Using = in if condition causes an error or unexpected behavior.
Final Answer:
Using = instead of == -> Option C
Quick Check:
Comparison needs ==, not = [OK]
Hint: Use == for comparison inside if() [OK]
Common Mistakes:
Using = instead of ==
Ignoring syntax errors from wrong operator
Thinking = works for comparison
5. You want to filter a vector v <- c(3, 5, 7, 9) to keep only values greater than or equal to 5. Which R expression correctly uses comparison operators to do this?
hard
A. v[v => 5]
B. v[v >= 5]
C. v[v = 5]
D. v[v >== 5]
Solution
Step 1: Understand filtering with comparison
To keep values >= 5, use the operator >= inside the indexing brackets.
Step 2: Check syntax correctness
=>, =, and >== are invalid operators in R.
Final Answer:
v[v >= 5] -> Option B
Quick Check:
Use >= for greater or equal in filtering [OK]
Hint: Use >= for greater or equal comparisons [OK]
Common Mistakes:
Using => which is invalid
Using = instead of comparison operator
Combining > and == incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.