0
0
R Programmingprogramming~15 mins

If-else statements in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
If-else statements
📖 Scenario: You are helping a small shop owner decide if a product is affordable for customers based on its price.
🎯 Goal: Build a simple R program that uses if-else statements to check if a product price is affordable or expensive compared to a set budget.
📋 What You'll Learn
Create a variable with the product price
Create a variable with the budget limit
Use an if-else statement to compare the price with the budget
Print 'Affordable' if the price is less than or equal to the budget
Print 'Expensive' if the price is greater than the budget
💡 Why This Matters
🌍 Real World
Shop owners often need to check if product prices fit within customer budgets to decide promotions or discounts.
💼 Career
Understanding if-else statements is essential for programming logic used in data analysis, automation, and decision-making tasks.
Progress0 / 4 steps
1
Create the product price variable
Create a variable called price and set it to 120.
R Programming
Need a hint?

Use the assignment operator <- to assign the value 120 to price.

2
Create the budget variable
Create a variable called budget and set it to 100.
R Programming
Need a hint?

Use the assignment operator <- to assign the value 100 to budget.

3
Write the if-else statement
Write an if-else statement that checks if price is less than or equal to budget. If yes, assign the string 'Affordable' to a variable called result. Otherwise, assign 'Expensive' to result.
R Programming
Need a hint?

Use if (price <= budget) { ... } else { ... } to decide the value of result.

4
Print the result
Write a line to print the value of the variable result.
R Programming
Need a hint?

Use print(result) to show the result on the screen.