Assignment Week 4
Data Science Programming 1
& Statistical Computing
1 Dataset
| ID | Name | Age | Salary | Position | Performance |
|---|---|---|---|---|---|
| 1 | Bagas | 25 | 5000 | Staff | Good |
| 2 | Joan | 30 | 7000 | Supervisor | Very Good |
| 3 | Alya | 27 | 6500 | Staff | Average |
| 4 | Dwi | 35 | 10000 | Manager | Good |
| 5 | Nabil | 40 | 12000 | Director | Very Good |
2 Conditional Statement
Determine bonus levels based on employee performance:
- Very Good → 20% of salary
- Good → 10% of salary
- Average → 5% of salary
Your Task: Write a program in R to calculate each employee’s bonus.
# Employee data
employee <- data.frame(
ID = 1:5,
Name = c("Bagas", "Joan", "Alya", "Dwi", "Nabil"),
Age = c(25, 30, 27, 35, 40),
Salary = c(5000, 7000, 6500, 10000, 12000),
Position = c("Staff", "Supervisor", "Staff", "Manager", "Director"),
Performance = c("Good", "Very Good", "Average", "Good", "Very Good"),
stringsAsFactors = FALSE
)
# Calculate bonus based on performance level:explain the logic of calculating bonuses
# - Very Good = 20% of salary
# - Good = 10% of salary
# - Average = 5% of salary
employee$Bonus <- ifelse(employee$Performance == "Very Good", employee$Salary * 0.20,
ifelse(employee$Performance == "Good", employee$Salary * 0.10,
ifelse(employee$Performance == "Average", employee$Salary * 0.05, 0)))
# Print each employee's name and bonus one by one
for (i in 1:nrow(employee)) {
cat("Name:", employee$Name[i], ", Bonus:", employee$Bonus[i], "\n")
}Name: Bagas , Bonus: 500
Name: Joan , Bonus: 1400
Name: Alya , Bonus: 325
Name: Dwi , Bonus: 1000
Name: Nabil , Bonus: 2400
3 Loops (For and While)
Loops automate repetitive tasks for better code efficiency. The implementation includes For Loops (fixed iterations), While Loops (conditional), and Break and Continue statements to flexibly control data flow based on dataset criteria.
3.1 For loop: Salary >6000
For Loop is used to iterate through a dataset where the number of iterations is already determined. In this task, the loop functions to check each row of the dataset sequentially and execute filtering logic based on specific predefined criteria.
In the output provided below, the loop is implemented to filter employees with a salary greater than 6000. The program traverses the entire dataset and only displays those who meet the condition, specifically Joan, Alya, Dwi, and Nabil. This process demonstrates how a for loop can automate the scanning of large datasets quickly and accurately.
# Employee data
employee <- data.frame(
ID = 1:5,
Name = c("Bagas", "Joan", "Alya", "Dwi", "Nabil"),
Age = c(25, 30, 27, 35, 40),
Salary = c(5000, 7000, 6500, 10000, 12000),
Position = c("Staff", "Supervisor", "Staff", "Manager", "Director"),
Performance = c("Good", "Very Good", "Average", "Good", "Very Good"),
stringsAsFactors = FALSE
)
# Loop through each employee one by one
for (i in 1:nrow(employee)) {
# Only print employees whose salary is greater than 6000
if (employee$Salary[i] > 6000) {
cat("Name:", employee$Name[i], ", Salary:", employee$Salary[i], "-> Above 6000\n")
}
}Name: Joan , Salary: 7000 -> Above 6000
Name: Alya , Salary: 6500 -> Above 6000
Name: Dwi , Salary: 10000 -> Above 6000
Name: Nabil , Salary: 12000 -> Above 6000
3.2 While loop: Until the “manager” position
While Loop is used to execute a block of code as long as a specific condition remains true. Unlike a for loop, this structure continues to run without a fixed number of iterations until a termination requirement is met, making it highly effective for searching for specific entries within a dataset.
In the output provided below, the while loop is applied to scan the employee list step-by-step starting from the first row. The program continues to display names and positions until the system detects the “Manager” title. Once this position is found in Dwi’s data, the termination condition is met, and the search process stops automatically.
# Employee data
employee <- data.frame(
ID = 1:5,
Name = c("Bagas", "Joan", "Alya", "Dwi", "Nabil"),
Age = c(25, 30, 27, 35, 40),
Salary = c(5000, 7000, 6500, 10000, 12000),
Position = c("Staff", "Supervisor", "Staff", "Manager", "Director"),
Performance = c("Good", "Very Good", "Average", "Good", "Very Good"),
stringsAsFactors = FALSE
)
# Start from the first employee
i <- 1
# Loop will stop when it reaches Manager position
while (employee$Position[i] != "Manager") {
# Print current employee name and position
cat("Name:", employee$Name[i], ", Position:", employee$Position[i], "\n")
# Move to next employee
i <- i + 1
}Name: Bagas , Position: Staff
Name: Joan , Position: Supervisor
Name: Alya , Position: Staff
3.3 Break: Sallary >10000
Break is a control statement used to terminate the entire looping process prematurely, even if the loop’s original condition has not yet been fully met. This command is particularly useful when we need to exit a cycle immediately once a specific target is found or a critical threshold is reached.
In the output provided below, the break instruction is applied to monitor the employee salary threshold. The program processes the data sequentially until it reaches Nabil’s data, which shows a salary of 12,000. Since this value exceeds the 10,000 limit, the break command is immediately triggered to stop all looping activities, ensuring that no further data is processed by the system.
# Employee data
employee <- data.frame(
ID = 1:5,
Name = c("Bagas", "Joan", "Alya", "Dwi", "Nabil"),
Age = c(25, 30, 27, 35, 40),
Salary = c(5000, 7000, 6500, 10000, 12000),
Position = c("Staff", "Supervisor", "Staff", "Manager", "Director"),
Performance = c("Good", "Very Good", "Average", "Good", "Very Good"),
stringsAsFactors = FALSE
)
# Loop through each employee one by one
for (i in 1:nrow(employee)) {
# If salary is above 10000, stop the loop immediately
if (employee$Salary[i] > 10000) {
cat("Stopped! Salary above 10000 found ->", "Name:", employee$Name[i], ", Salary:", employee$Salary[i], "\n")
break
}
# Print employee name and salary if salary is not above 10000
cat("Name:", employee$Name[i], ", Salary:", employee$Salary[i], "\n")
}Name: Bagas , Salary: 5000
Name: Joan , Salary: 7000
Name: Alya , Salary: 6500
Name: Dwi , Salary: 10000
Stopped! Salary above 10000 found -> Name: Nabil , Salary: 12000
3.4 Continue (Next): Skip performance “Average”
Continue (or Next in R) is a control statement used to skip the remaining code within the current iteration and move immediately to the next one. Unlike break, which terminates the entire loop, this command only skips a specific step that does not meet the criteria without stopping the overall looping cycle.
Continue (or Next in R) is a control statement used to skip the remaining code within the current iteration and move immediately to the next one. Unlike break, which terminates the entire loop, this command only skips a specific step that does not meet the criteria without stopping the overall looping cycle.
# Employee data
employee <- data.frame(
ID = 1:5,
Name = c("Bagas", "Joan", "Alya", "Dwi", "Nabil"),
Age = c(25, 30, 27, 35, 40),
Salary = c(5000, 7000, 6500, 10000, 12000),
Position = c("Staff", "Supervisor", "Staff", "Manager", "Director"),
Performance = c("Good", "Very Good", "Average", "Good", "Very Good"),
stringsAsFactors = FALSE
)
# Loop through each employee one by one
for (i in 1:nrow(employee)) {
# If performance is "Average", skip and move to next employee
if (employee$Performance[i] == "Average") {
cat("Skipped:", employee$Name[i], "-> Performance: Average\n")
next
}
# Print employee name and performance if not "Average"
cat("Name:", employee$Name[i], ", Performance:", employee$Performance[i], "\n")
}Name: Bagas , Performance: Good
Name: Joan , Performance: Very Good
Skipped: Alya -> Performance: Average
Name: Dwi , Performance: Good
Name: Nabil , Performance: Very Good
4 Conclusion
This practicum demonstrates that Conditional Statements (if, if-elif, else) and Loops (for, while) are essential for creating dynamic programs. Conditional logic enables accurate data-driven decision-making, while loops—combined with Break and Continue statements—provide efficiency in processing and filtering large amounts of information automatically and systematically.
5 References
Siregar, B. (n.d.). Syntax and Control Flow. In Data Science Programming. Data Science Labs. https://bookdown.org/dsciencelabs/data_science_programming/02-Syntax-and-Control-Flow.html