Data Science Programming
Prakticum Week 4
1 Dataset
# Create employee dataset
employees <- data.frame(
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
)
# Display the dataset as a colored table
library(kableExtra)
kable(employees, caption = "Employee Dataset") %>%
kable_styling(bootstrap_options = c("striped", "hover", "bordered")) %>%
row_spec(0, background = "#FF69B4", color = "white", bold = TRUE) %>%
row_spec(1:nrow(employees), background = "#FFE4E1")| name | age | salary | position | performance |
|---|---|---|---|---|
| Bagas | 25 | 5000 | Staff | Good |
| Joan | 30 | 7000 | Supervisor | Very Good |
| Alya | 27 | 6500 | Staff | Average |
| Dwi | 35 | 10000 | Manager | Good |
| Nabil | 40 | 12000 | Director | Very Good |
2 Task 1 - Conditional Statement
Calculate each employee’s bonus based on their performance:
- Very Good → 20% of salary
- Good → 10% of salary
- Average → 5% of salary
# Task 1: Calculate bonus using if-else conditional statement
for (i in 1:nrow(employees)) {
perf <- employees$performance[i]
salary <- employees$salary[i]
name <- employees$name[i]
# Check performance level and assign bonus accordingly
if (perf == "Very Good") {
bonus <- salary * 0.20 # Very Good = 20% of salary
} else if (perf == "Good") {
bonus <- salary * 0.10 # Good = 10% of salary
} else {
bonus <- salary * 0.05 # Average = 5% of salary
}
cat(sprintf("Name: %s, Bonus: %d\n", name, as.integer(bonus)))
}## Name: Bagas, Bonus: 500
## Name: Joan, Bonus: 1400
## Name: Alya, Bonus: 325
## Name: Dwi, Bonus: 1000
## Name: Nabil, Bonus: 2400
3 Task 2 - For Loop
Use a for loop to display employees with a salary greater than 6000.
# Task 2: Display employees with salary above 6000 using for loop
for (i in 1:nrow(employees)) {
# Only display if salary is greater than 6000
if (employees$salary[i] > 6000) {
cat(sprintf("Name: %s, Salary: %d\n",
employees$name[i],
employees$salary[i]))
}
}## Name: Joan, Salary: 7000
## Name: Alya, Salary: 6500
## Name: Dwi, Salary: 10000
## Name: Nabil, Salary: 12000
4 Task 3 - While Loop
Use a while loop to display employees until the position “Manager” is found.
# Task 3: Display employees using while loop, stop when Manager is found
i <- 1 # Start index from 1 (R index starts from 1, not 0)
while (i <= nrow(employees)) {
cat(sprintf("Name: %s, Position: %s\n",
employees$name[i],
employees$position[i]))
# Stop the loop when Manager position is found
if (employees$position[i] == "Manager") {
break
}
i <- i + 1 # Move to the next employee
}## Name: Bagas, Position: Staff
## Name: Joan, Position: Supervisor
## Name: Alya, Position: Staff
## Name: Dwi, Position: Manager
5 Task 4 - Break
Use break to stop the loop when an employee with a salary above 10,000 is found.
# Task 4: Stop the loop using break when salary exceeds 10,000
for (i in 1:nrow(employees)) {
# If salary exceeds 10,000, print message and stop the loop
if (employees$salary[i] > 10000) {
cat(sprintf("(Stopped because %s has a salary above 10,000)\n",
employees$name[i]))
break
}
cat(sprintf("Name: %s, Salary: %d\n",
employees$name[i],
employees$salary[i]))
}## 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)
6 Task 5 - Continue
Use next to skip employees with “Average” performance.
# Task 5: Skip employees with Average performance using next (equivalent to continue in Python)
for (i in 1:nrow(employees)) {
# Skip this iteration if performance is Average
if (employees$performance[i] == "Average") {
next
}
cat(sprintf("Name: %s, Performance: %s\n",
employees$name[i],
employees$performance[i]))
}## 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")