Practicum Week 4
Naisya Hafizh Mufidah
NIM = 52250040
Dosen Pengampu = Mr. Bakti Siregar, M.Sc., CDS.
1 Data Dummy
library(knitr)
# dummy dataset
employees <- data.frame(
ID = c("1", "2", "3", "4", "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"))
kable(employees)| 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 Statements
Determine bonus levels based on employee performance:
- Very Good = 20% of salary
- Good = 10% of salary
- Average = 5% of salary
library(knitr)
bonus <- numeric(nrow(employees))
for(i in 1:nrow(employees)){
salary <- employees$Salary[i]
performance <- employees$Performance[i]
# conditional statement
if(performance == "Very Good"){
bonus[i] <- salary * 0.20
} else if(performance == "Good"){
bonus[i] <- salary * 0.10
} else {
bonus[i] <- salary * 0.05
}
}
# membuat tabel hasil
bonus_table <- data.frame(
Name = employees$Name,
Bonus = bonus
)
kable(bonus_table)| Name | Bonus |
|---|---|
| Bagas | 500 |
| Joan | 1400 |
| Alya | 325 |
| Dwi | 1000 |
| Nabil | 2400 |
## Name: Bagas , Bonus: 500
## Name: Joan , Bonus: 1400
## Name: Alya , Bonus: 325
## Name: Dwi , Bonus: 1000
## Name: Nabil , Bonus: 2400
Interpretation
This code calculates the bonus for each employee based on their performance. The program checks the performance level using conditional statements (if, elif, else). Employees with Very Good performance receive 20% of their salary, Good receives 10%, and Average receives 5%. The result shows each employee’s name and their bonus.
3 Loops (For & While)
- Use a for loop to list employees with a salary greater than 6000.
for(i in 1:nrow(employees)){
if(employees$Salary[i] > 6000){
cat("Name:", employees$Name[i], ", Salary:", employees$Salary[i], "\n")
}
}## Name: Joan , Salary: 7000
## Name: Alya , Salary: 6500
## Name: Dwi , Salary: 10000
## Name: Nabil , Salary: 12000
Interpretation
This code uses a for loop to go through each employee in the dataset. The program checks whether the employee’s salary is greater than 6000. If the condition is true, the employee’s name and salary are displayed. Bagas not on the list because his salary = 5000 under than 6000.
- Use while loop to display employees until a “Manager” is found.
i <- 1
while(i <= nrow(employees)){
if(employees$Position[i] == "Manager"){
cat("Name:", employees$Name[i], ", Position:", employees$Position[i], "(Stop here)\n")
break
}
cat("Name:", employees$Name[i], ", Position:", employees$Position[i], "\n")
i <- i + 1
}## Name: Bagas , Position: Staff
## Name: Joan , Position: Supervisor
## Name: Alya , Position: Staff
## Name: Dwi , Position: Manager (Stop here)
Interpretation
This code uses a while loop to display employees one by one until a Manager is found. When the position “Manager” appears, the program prints the employee’s information and stops the loop.
- Use break to stop the loop when an employee with a salary above 10,000 is found.
for(i in 1:nrow(employees)){
if(employees$Salary[i] > 10000){
cat("(Stopped because", employees$Name[i], "has a salary above 10,000)\n")
break
}
cat("Name:", employees$Name[i], ", Salary:", employees$Salary[i], "\n")
}## Name: Bagas , Salary: 5000
## Name: Joan , Salary: 7000
## Name: Alya , Salary: 6500
## Name: Dwi , Salary: 10000
## (Stopped because Nabil has a salary above 10,000)
Interpretation
This code demonstrates the use of the break statement. The loop checks each employee’s salary and stops when it finds a salary above 10,000. A message is displayed explaining why the loop stopped.
- Use continue to skip employees with “Average” performance.
# tempat menyimpan nama yang di-skip
skipped <- c()
for(i in 1:nrow(employees)){
if(employees$Performance[i] == "Average"){
# simpan nama yang di-skip
skipped <- c(skipped, employees$Name[i])
next
}
cat("Name:", employees$Name[i], ", Performance:", employees$Performance[i], "\n")
}## Name: Bagas , Performance: Good
## Name: Joan , Performance: Very Good
## Name: Dwi , Performance: Good
## Name: Nabil , Performance: Very Good
## ( Alya is skipped because the performance is 'Average')
Interpretation
This code uses the continue statement to skip employees with Average performance. Their names are stored, but they are not printed in the main list. At the end, the program shows which employee was skipped.
4 Conclusion
In this practicum, conditional statements and loops were used to process employee data. Conditional statements helped determine employee bonuses based on their performance, while loops (for and while) were used to iterate through the dataset and apply specific conditions. Additionally, the use of break and continue demonstrated how loops can be controlled to stop or skip certain data during program execution.
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