Practicum DS Programming Week 4
Angelique Kiyoshi Lakeisha B.U
NIM: 52250001
Student Major Data Science at Institut Teknologi Sains Bandung
1 Employee Dataset
The following dataset represents a simple employee record used for this exercise. It includes basic information such as employee ID, name, age, salary, position, and performance evaluation. This dataset will be used to demonstrate conditional statements and loop operations in R.
2 Conditional Statements
Conditional statements allow a program to execute different actions
depending on specific conditions. In programming, this logic is commonly
implemented using structures such as if,
else if, and else statements.
2.1 Bonus Rules
| Performance | Bonus |
|---|---|
| Very Good | 20% of salary |
| Good | 10% of salary |
| Average | 5% of salary |
2.2 Determine Bonus Levels
case_when() function from the dplyr package, which works similarly to multiple if–else conditions. Bonus for each employee is calculated by multiplying the bonus percentage by the employee's salary.
# menghitung bonus berdasarkan performance
bonus_results <- employees %>%
mutate(
Bonus_Percent = case_when(
Performance == "Very Good" ~ "20%",
Performance == "Good" ~ "10%",
TRUE ~ "5%"
),
Bonus = case_when(
Performance == "Very Good" ~ Salary * 0.20,
Performance == "Good" ~ Salary * 0.10,
TRUE ~ Salary * 0.05
)
)The resulting bonus values for each employee are presented in the table below:
Interpretation:
The table shows the calculated bonus for each employee based on their performance evaluation. Employees with higher performance ratings receive larger bonus amounts, with Nabil receiving the highest bonus due to his high salary and “Very Good” performance.
2.3 Visualization
Interpretation:
The bar chart illustrates the distribution of bonus values among employees. It shows that employees with higher salaries and better performance ratings receive larger bonus amounts.
3 Loops (For and While)
Loops are used to repeat a block of code multiple times based on a specified condition. In this section, different looping techniques are applied to analyze the employee dataset. These examples demonstrate how loops can be used to filter data, stop execution based on certain conditions, and skip specific records.
3.1 For Loop
for syntax to iterate through a sequence of values. In this exercise, the for(i in 1:nrow(employees)) loop is used to iterate through the employee dataset and display employees whose salary is greater than 6000.
high_salary_employees <- data.frame() # membuat list kosong untuk menyimpan hasil
for(i in 1:nrow(employees)){ # melakukan perulangan untuk setiap baris data
salary <- employees$Salary[i] # mengambil nilai salary
if(salary > 6000){ # mengecek apakah salary lebih besar dari 6000
high_salary_employees <- rbind( # jika benar, data karyawan dimasukkan ke hasil
high_salary_employees,
employees[i, c("Name","Salary")]
)
}
}The employees who meet this condition are displayed in the table below:
Interpretation:
The table shows employees whose salary is greater than 6000. Bagas is not included in the result because his salary (5000) does not satisfy the condition defined in the loop.
3.2 While Loop
while syntax to repeatedly execute a block of code as long as a condition remains true. In this example, while(index <= nrow(employees)) is used to iterate through the dataset until an employee with the position "Manager" is found.
employees_until_manager <- data.frame() # membuat tabel kosong untuk menyimpan hasil
index <- 1 # membuat index awal
while(index <= nrow(employees)){ # menjalankan while loop selama index masih dalam jumlah data
employee <- employees[index,] # mengambil data karyawan berdasarkan index
employees_until_manager <- rbind( # menyimpan nama dan posisi ke tabel hasil
employees_until_manager,
employee[,c("Name","Position")]
)
if(employee$Position == "Manager"){ # mengecek apakah posisi karyawan adalah Manager
break # jika Manager ditemukan, loop berhenti
}
index <- index + 1 # menambah index agar membaca data berikutnya
}The employees listed during the iteration until a “Manager” is found are shown in the table below:
Interpretation:
The loop stops once an employee with the position “Manager” is found. Therefore, only employees from the beginning of the dataset until Dwi are displayed, while Nabil does not appear because the loop terminates when the Manager position is encountered.
3.3 Break Statement
break stops the iteration once an employee with a salary greater than 10,000 is encountered.
employees_before_high_salary <- data.frame() # membuat tabel kosong untuk menyimpan hasil
for(i in 1:nrow(employees)){ # melakukan perulangan membaca setiap karyawan
salary <- employees$Salary[i] # mengambil nilai salary
if(salary > 10000){ # mengecek apakah salary lebih dari 10000
break # jika ditemukan salary > 10000, loop berhenti
}
employees_before_high_salary <- rbind( # jika salary belum > 10000, data disimpan
employees_before_high_salary,
employees[i, c("Name","Salary")]
)
}The employees processed before the loop stops are shown in the table below.
Interpretation:
The loop stops when an employee with a salary greater than 10,000 is encountered. Since Nabil has a salary of 12,000, the loop terminates at that point, and therefore Nabil is not included in the result table.
3.4 Next (Continue)
next is used to skip employees whose performance is classified as "Average".
filtered_employees <- data.frame() # membuat tabel kosong untuk menyimpan hasil
for(i in 1:nrow(employees)){ # melakukan perulangan untuk setiap karyawan
performance <- employees$Performance[i] # mengambil nilai performance
if(performance == "Average"){ # mengecek apakah performance adalah "Average"
next # jika Average, maka data dilewati
}
filtered_employees <- rbind( # jika bukan Average, data disimpan
filtered_employees,
employees[i, c("Name","Performance")]
)
}The remaining employees after skipping those with “Average” performance are displayed in the table below:
Interpretation:
Employees with “Average” performance are skipped during the iteration
using the next statement. As a result, Alya does not appear
in the table because her performance rating is classified as
“Average”.
This exercise demonstrates the use of conditional statements and
looping structures in R for basic data processing tasks. By applying
functions such as case_when(), for, and
while, the program can evaluate conditions, perform
repeated operations, and control the flow of execution using statements
like break and next. These fundamental
programming concepts are essential for automating data manipulation and
analysis in R.