📖 Scenario: You are working with a list of students' scores. You want to quickly label each score as "Pass" or "Fail" based on a passing mark.
🎯 Goal: Create a vector of scores, set a passing threshold, use the ifelse function to label each score as "Pass" or "Fail", and print the result.
📋 What You'll Learn
Create a numeric vector called scores with the values 45, 67, 89, 34, 56
Create a numeric variable called pass_mark and set it to 50
Use the ifelse function with scores and pass_mark to create a character vector called results labeling scores as "Pass" or "Fail"
Print the results vector
💡 Why This Matters
🌍 Real World
Teachers and trainers often need to quickly classify student scores to identify who passed or failed.
💼 Career
Data analysts and statisticians use vectorized functions like <code>ifelse</code> in R to efficiently process and categorize data.
Progress0 / 4 steps
1
Create the scores vector
Create a numeric vector called scores with these exact values: 45, 67, 89, 34, 56
R Programming
Hint
Use the c() function to combine numbers into a vector.
2
Set the passing mark
Create a numeric variable called pass_mark and set it to 50
R Programming
Hint
Assign the number 50 to the variable pass_mark using the assignment operator <-.
3
Use ifelse to label pass or fail
Use the ifelse function with scores and pass_mark to create a character vector called results that contains "Pass" if the score is greater than or equal to pass_mark, otherwise "Fail"
R Programming
Hint
The ifelse function takes three arguments: a condition, a value if true, and a value if false.
4
Print the results vector
Print the results vector using the print() function
R Programming
Hint
Use print(results) to show the vector on the screen.
Practice
(1/5)
1. What does the ifelse function in R do?
easy
A. It checks a condition for each element and returns values accordingly.
B. It creates a loop to iterate over elements.
C. It sorts a vector in ascending order.
D. It combines two vectors into one.
Solution
Step 1: Understand the purpose of ifelse
The ifelse function tests a condition for each element in a vector.
Step 2: Identify the output behavior
It returns a new vector with values chosen from the 'yes' or 'no' options based on the condition.
Final Answer:
It checks a condition for each element and returns values accordingly. -> Option A
Quick Check:
ifelse = condition-based selection [OK]
Hint: Think: condition check for each item returns chosen values [OK]
Common Mistakes:
Confusing ifelse with loops
Thinking it sorts or merges vectors
Assuming it modifies original vector in place
2. Which of the following is the correct syntax for using ifelse in R?
easy
A. ifelse(condition, yes, no)
B. ifelse(yes, no, condition)
C. ifelse(condition; yes; no)
D. ifelse(condition -> yes, no)
Solution
Step 1: Recall the function signature
The ifelse function takes three arguments: a condition, a value if true, and a value if false.
Step 2: Match the correct order and separators
The correct syntax uses commas to separate arguments: ifelse(condition, yes, no).
Final Answer:
ifelse(condition, yes, no) -> Option A
Quick Check:
Syntax = ifelse(condition, yes, no) [OK]
Hint: Remember: condition first, then yes, then no, separated by commas [OK]
C. Using print() instead of cat() to display output.
D. Missing comma between 'high' and 'low' in ifelse arguments.
Solution
Step 1: Check the ifelse argument syntax
The ifelse function requires three arguments separated by commas: condition, yes, no.
Step 2: Identify missing comma
In the code, "high" and "low" are not separated by a comma, causing a syntax error.
Final Answer:
Missing comma between 'high' and 'low' in ifelse arguments. -> Option D
Quick Check:
Arguments must be comma-separated [OK]
Hint: Check commas between all ifelse arguments [OK]
Common Mistakes:
Omitting commas between arguments
Confusing operators > and >=
Thinking print() causes error here
5. You have a numeric vector temps <- c(-5, 0, 10, 20, 30). Use ifelse to create a new vector labels that labels temperatures below 0 as "freezing", between 0 and 20 (inclusive) as "cold", and above 20 as "warm". Which code correctly does this?