In this practicum, an employee dataset is used to demonstrate how conditional statements and loops work in data analysis. Conditional statements allow the program to make decisions based on certain conditions in the dataset, while loops allow the program to repeatedly process data efficiently.
The dataset contains information about employees, including their name, age, salary, position, and performance level. By applying conditional statements and loops, the dataset can be analyzed to identify specific conditions such as salary levels, employee bonuses, and performance filtering.
The following dummy dataset will be used in this practicum:
| 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 |
Conditional statements are used to determine decisions based on certain conditions in the dataset. In this case, the program determines the bonus level of each employee based on their performance.
Bonus Rules
A bonus is an additional amount of money given to employees beyond their regular salary as a reward for their performance. In this task, the bonus is calculated based on employee performance levels. Employees with better performance receive a higher percentage of bonus. This system is useful to motivate employees to work better and to recognize their contributions to the company.
| Performance | Bonus |
|---|---|
| Very Good | 20% of Salary |
| Good | 10% of Salary |
| Average | 5% of Salary |
Determine Bonus Levels
How to Determine Bonus Levels
To determine the bonus levels of employees, we use conditional statements in R:
if statements to check each performance category.
if-else and if-else if-else to assign different percentages based on performance.
for loop to iterate through each employee in the dataset.
Calculations are performed by multiplying the salary by the percentage corresponding to the performance level.
By combining these techniques, we can systematically calculate each employee’s bonus based on their performance level.
# Create dataset
employees <- data.frame(
Name = c("Bagas","Joan","Alya","Dwi","Nabil"),
Salary = c(5000,7000,6500,10000,12000),
Performance = c("Good","Very Good","Average","Good","Very Good")
)
# Tentukan bonus
bonus <- c() # vector kosong untuk menampung bonus
for(i in 1:nrow(employees)){
if(employees$Performance[i] == "Very Good"){
bonus[i] <- employees$Salary[i] * 0.20
} else if(employees$Performance[i] == "Good"){
bonus[i] <- employees$Salary[i] * 0.10
} else { # Average
bonus[i] <- employees$Salary[i] * 0.05
}
}
# Tambahkan ke dataset
employees$Bonus <- bonus
employees$BonusPercent <- ifelse(employees$Performance=="Very Good", "20%",
ifelse(employees$Performance=="Good","10%","5%"))
| Name | Salary | Performance | BonusPercent | Bonus |
|---|---|---|---|---|
| Bagas | 5000 | Good | 10% | 500 |
| Joan | 7000 | Very Good | 20% | 1400 |
| Alya | 6500 | Average | 5% | 325 |
| Dwi | 10000 | Good | 10% | 1000 |
| Nabil | 12000 | Very Good | 20% | 2400 |
Interpretation
Based on the table and visualization, employees with ‘Very Good’ performance receive the highest bonus, while employees with ‘Average’ performance receive the lowest bonus. Nabil is the employee who received the highest bonus, which is 12000. Bonuses are influenced by a combination of salary and performance level. The average bonus received by employees is $1125.
Loops or iterations are used to process data automatically row by row. In this example, an employee dataset is used to Identify employees with certain salaries (for loop), Display employees until a certain position is found (while loop), Stop the loop if a certain condition is met (break), Skip employees with certain conditions (continue).
For Loop
In this section, we use a for loop to analyze the employee dataset. The goal is to identify employees whose salary is greater than 6000. A for loop allows to systematically iterate through each row of the dataset, check the salary condition, and store the qualified employees in a new table. This approach is useful for processing datasets row by row and applying conditional logic efficiently in R.
# Chunk 1: For loop untuk ambil karyawan dengan Salary > 6000
library(dplyr)
employees <- data.frame(
Name = c("Bagas","Joan","Alya","Dwi","Nabil"),
Salary = c(5000,7000,6500,10000,12000),
Performance = c("Good","Very Good","Average","Good","Very Good")
)
# For loop: filter salary > 6000
high_salary <- data.frame(Name=character(), Salary=numeric(), stringsAsFactors=FALSE)
for(i in 1:nrow(employees)){
if(employees$Salary[i] > 6000){
high_salary <- rbind(high_salary, data.frame(Name=employees$Name[i], Salary=employees$Salary[i]))
}
}
Employees with Salary > 6000 :
| Name | Salary |
|---|---|
| Joan | 7000 |
| Alya | 6500 |
| Dwi | 10000 |
| Nabil | 12000 |
Employees with a salary greater than 6000 are Joan, Alya, Dwi, and Nabil.
Higher salaries are usually held by more experienced or senior-position employees.
While Loop
In this section, a while loop is used to display employees consecutively until a “Manager” is found. Unlike a for loop, a while loop is useful when the number of iterations is not known beforehand. This loop continues as long as the specified condition remains true. Here, the loop iterates through the dataset row by row and stops once it finds an employee whose position is “Manager”, storing all employees encountered up to that point in a new table.
library(dplyr)
# Dataset
employees <- data.frame(
Name = c("Bagas","Joan","Alya","Dwi","Nabil"),
Salary = c(5000,7000,6500,10000,12000),
Performance = c("Good","Very Good","Average","Good","Very Good"),
Position = c("Staff","Supervisor","Staff","Manager","Director")
)
# While loop: display employees until 'Manager'
while_list <- data.frame(Name=character(), Position=character(), stringsAsFactors=FALSE)
i <- 1
while(i <= nrow(employees)){
while_list <- rbind(while_list, data.frame(Name=employees$Name[i], Position=employees$Position[i]))
if(employees$Position[i] == "Manager"){ break }
i <- i + 1
}
Employees until Manager:
| Name | Position |
|---|---|
| Bagas | Staff |
| Joan | Supervisor |
| Alya | Staff |
| Dwi | Manager |
If the employee's position is not "Manager," the loop will continue to the next employee, adding them to the table. Once the loop finds an employee whose position is "Manager," the loop stops immediately, and no other employees are added. This demonstrates how a while loop can control execution based on a condition: the loop keeps repeating as long as the condition is true, and stops when the condition is met, effectively capturing all employees up to and including the first Manager. In the table, the loop stops because it has found an employee named Dwi with the position of "Manager".
Break
In this section, a break statement is used inside a loop to stop the iteration once a certain condition is met. This loop iterates through the employee dataset and adds each employee to the table. If an employee has a salary greater than 10,000, the loop will immediately stop and no other employees will be added. This demonstrates how the break statement allows exiting the loop early based on a predetermined condition.
# Dataset
employees <- data.frame(
Name = c("Bagas","Joan","Alya","Dwi","Nabil"),
Salary = c(5000,7000,6500,10000,12000),
Performance = c("Good","Very Good","Average","Good","Very Good")
)
# Break loop: stop when Salary > 10000
break_list <- data.frame(Name=character(), Salary=numeric(), stringsAsFactors=FALSE)
for(i in 1:nrow(employees)){
if(employees$Salary[i] > 10000){
break
}
break_list <- rbind(break_list, data.frame(Name=employees$Name[i], Salary=employees$Salary[i]))
}
Employees until Salary > 10,000:
| Name | Salary |
|---|---|
| Bagas | 5000 |
| Joan | 7000 |
| Alya | 6500 |
| Dwi | 10000 |
The loop adds employees to the table until it encounters an employee with a salary above 10,000.
Once the loop reaches Dwi with a salary of 10,000, the break statement immediately stops the loop.
No further employees are added after this point, demonstrating how break can control loop execution by exiting early when a condition is met.
Continue
In this section, the continue statement is used inside a loop to skip certain employees based on a condition. The loop iterates through the dataset and adds employees to the table only if their performance is not “Average.” If an employee has “Average” performance, the loop will skip them and move on to the next employee. This demonstrates how continue allows selective execution within a loop, allowing certain iterations to be ignored without stopping the entire loop.
# Dataset
employees <- data.frame(
Name = c("Bagas","Joan","Alya","Dwi","Nabil"),
Salary = c(5000,7000,6500,10000,12000),
Performance = c("Good","Very Good","Average","Good","Very Good")
)
# Continue loop: skip employees with "Average" performance
continue_list <- data.frame(Name=character(), Performance=character(), stringsAsFactors=FALSE)
for(i in 1:nrow(employees)){
if(employees$Performance[i] == "Average"){
next # skip this iteration
}
continue_list <- rbind(continue_list, data.frame(Name=employees$Name[i], Performance=employees$Performance[i]))
}
Employees Skipping Average Performance:
| Name | Performance |
|---|---|
| Bagas | Good |
| Joan | Very Good |
| Dwi | Good |
| Nabil | Very Good |
The loop iterates through all employees and adds them to the table only if their performance is not "Average".
Alya is skipped because her performance is "Average", while Bagas, Joan, Dwi, and Nabil are included.
This demonstrates how the continue statement allows selective execution, skipping certain iterations without stopping the loop entirely.
In R, conditional statements and loops are essential for automating data processing and decision-making. Conditional statements allow us to apply rules, like calculating bonuses based on performance. Loops let us iterate over datasets to filter, select, or skip data based on conditions. For example, the for loop identifies employees with salaries above a threshold, the while loop stops at a certain condition, the break statement exits early when needed, and the continue statement skips unwanted entries. Together, these tools make data analysis efficient, repeatable, and precise.