DATA SCIENCE PROGRAMMING
Practicum Week 4
1 Dataset
Create a Dummy Dataset with using dataframe and library kableExtra to create a table
# Create a data frame
data_emp <- 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"),
stringsAsFactors = FALSE
)
# Add kableExtra for create a table
library(kableExtra)
kable(data_emp, caption = "Dummy Dataset Employee") %>%
kable_styling(bootstrap_options = c("striped", "hover", "bordered")) %>%
row_spec(0, background = "purple", color = "bisque", bold = TRUE) %>%
row_spec(1:nrow(data_emp), background = "darkorchid3")| 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 level based on employee performance:
- Very Good -> 20% of salary
- Good -> 10% of salary
- Average -> 5% of salary
for (i in 1:nrow(data_emp)) {
name_emp <- data_emp$Name[i]
salary_emp <- data_emp$Salary[i]
performance_emp <- data_emp$Performance[i]
if (performance_emp == "Very Good") {
percentage_bonus <- 0.20
} else if (performance_emp == "Good") {
percentage_bonus <- 0.10
} else if (performance_emp == "Average") {
percentage_bonus <- 0.05
} else {
percentage_bonus <- 0
}
# Calculate Bonus
total_calculate <- salary_emp * percentage_bonus
cat(sprintf("Name: %s, Bonus: %g\n", name_emp, total_calculate))
}## Name: Bagas, Bonus: 500
## Name: Joan, Bonus: 1400
## Name: Alya, Bonus: 325
## Name: Dwi, Bonus: 1000
## Name: Nabil, Bonus: 2400
3 Loops (For & While)
Loops allow programs to repeat actions multiple times.
3.1 For Loop
Use For Loop to list employees with a salary greater than 6000.
for (i in 1:nrow(data_emp)) {
if (data_emp$Salary[i] > 6000) {
cat("Name:", data_emp$Name[i], ", Salary:", data_emp$Salary[i], "\n")
}
}## Name: Joan , Salary: 7000
## Name: Alya , Salary: 6500
## Name: Dwi , Salary: 10000
## Name: Nabil , Salary: 12000
3.2 While Loop
Use a While Loop to display employees until a โManagerโ is found.
for (i in 1:nrow(data_emp)) {
if (data_emp$Salary[i] > 6000) {
cat("Name:", data_emp$Name[i], ", Salary:", data_emp$Salary[i], "\n")
}
}## Name: Joan , Salary: 7000
## Name: Alya , Salary: 6500
## Name: Dwi , Salary: 10000
## Name: Nabil , Salary: 12000
3.3 Break
Use break to stop the loop when a employees with a salary above 10,000 is found.
for (i in 1:nrow(data_emp)) {
if (data_emp$Salary[i] > 10000) {
cat("(Stopped because", data_emp$Name[i], "has a salary above 10,000)\n")
break
}
cat("Name:", data_emp$Name[i], ", Salary:", data_emp$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)
3.4 Continue
Use Continue to skip employees with โAverageโ performance.
for (i in 1:nrow(data_emp)) {
if (data_emp$Performance[i] == "Average") {
next
}
cat("Name:", data_emp$Name[i], ", Performance:", data_emp$Performance[i], "\n")
}## Name: Bagas , Performance: Good
## Name: Joan , Performance: Very Good
## Name: Dwi , Performance: Good
## Name: Nabil , Performance: Very Good