Prakticum Week ~ 4
Sintaks dan Alur Kontrol
Hi, I'm Risky
Data Science Student
I turn raw numbers into meaningful insights through statistical modeling and data analysis. Passionate about R programming and machine learning, I make complex data easy to understand.
1 Dataset
This lab uses a sample dataset of employees as practice data for analyzing using conditional statements and loops.
library(readxl)
library(knitr)
dataset <- read_excel("dataset.xlsx")
kable(dataset, caption = "Sample Employee Dataset")| 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
This section uses a conditional statement to determine the employee bonus based on their performance level.
Determine the bonus amount based on employee performance:
- Very Good \(\rightarrow\) 20% of salary
- Good \(\rightarrow\) 10% of salary
- Average \(\rightarrow\) 5% of salary
library(knitr)
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"),
stringsAsFactors = FALSE
)
calculate_bonus <- function(salary, performance) {
if (performance == "Very Good") {
salary * 0.20
} else if (performance == "Good") {
salary * 0.10
} else {
salary * 0.05
}
}
employees$Bonus_Pct <- ifelse(
employees$Performance == "Very Good", "20%",
ifelse(employees$Performance == "Good", "10%", "5%")
)
employees$Bonus <- mapply(calculate_bonus, employees$Salary, employees$Performance)
for (i in seq_len(nrow(employees))) {
cat(sprintf("Name: %s, Bonus: %g\n", employees$Name[i], employees$Bonus[i]))
}## Name: Bagas, Bonus: 500
## Name: Joan, Bonus: 1400
## Name: Alya, Bonus: 325
## Name: Dwi, Bonus: 1000
## Name: Nabil, Bonus: 2400
2.1 Results in the Table
results <- data.frame(
ID = employees$ID,
Name = employees$Name,
Position = employees$Position,
Performance = employees$Performance,
Salary = formatC(employees$Salary, format = "f", digits = 0, big.mark = "."),
`% Bonus` = employees$Bonus_Pct,
Bonus = formatC(employees$Bonus, format = "f", digits = 0, big.mark = "."),
check.names = FALSE
)
kable(results, align = c("c","l","l","c","r","c","r"),
caption = "Employee Bonus Calculation Table")| ID | Name | Position | Performance | Salary | % Bonus | Bonus |
|---|---|---|---|---|---|---|
| 1 | Bagas | Staff | Good | 5.000 | 10% | 500 |
| 2 | Joan | Supervisor | Very Good | 7.000 | 20% | 1.400 |
| 3 | Alya | Staff | Average | 6.500 | 5% | 325 |
| 4 | Dwi | Manager | Good | 10.000 | 10% | 1.000 |
| 5 | Nabil | Director | Very Good | 12.000 | 20% | 2.400 |
3 Loops (For & While)
3.1 For Loop
This section uses a for loop to display employees whose salary is more than 6,000.
for (i in seq_len(nrow(dataset))) {
if (dataset$Salary[i] > 6000) {
cat(sprintf("Name: %s, Salary: %g\n",
dataset$Name[i],
dataset$Salary[i]
))
}
}## Name: Joan, Salary: 7000
## Name: Alya, Salary: 6500
## Name: Dwi, Salary: 10000
## Name: Nabil, Salary: 12000
salary_filter <- employees[employees$Salary > 6000, ]
results_table <- data.frame(
ID = salary_filter$ID,
Name = salary_filter$Name,
Age = salary_filter$Age,
Position = salary_filter$Position,
Performance = salary_filter$Performance,
Salary = formatC(salary_filter$Salary, format = "f", digits = 0, big.mark = ".")
)
kable(results_table, align = c("c","l","c","l","c","r"),
caption = "List of Employees with Salary > 6,000")| ID | Name | Age | Position | Performance | Salary |
|---|---|---|---|---|---|
| 2 | Joan | 30 | Supervisor | Very Good | 7.000 |
| 3 | Alya | 27 | Staff | Average | 6.500 |
| 4 | Dwi | 35 | Manager | Good | 10.000 |
| 5 | Nabil | 40 | Director | Very Good | 12.000 |
Interpretation
The code iterates through the dataset and displays the names and salaries of employees whose salary is greater than 6,000.
3.2 While Loop
This section uses a while loop to display employee data until a Manager position is found.
i <- 1
while (i <= nrow(dataset)) {
cat(sprintf("Name: %s, Position: %s\n",
dataset$Name[i],
dataset$Position[i]
))
if (dataset$Position[i] == "Manager") break
i <- i + 1
}## Name: Bagas, Position: Staff
## Name: Joan, Position: Supervisor
## Name: Alya, Position: Staff
## Name: Dwi, Position: Manager
Interpretation
The code displays the names and positions of employees in order and stops when a Manager position is found.
3.3 Break
This section uses a break statement to stop the loop when a certain condition is met.
for (i in seq_len(nrow(dataset))) {
if (dataset$Salary[i] > 10000) {
cat("Loop stopped! Found an employee with salary above 10,000:\n")
cat(sprintf("Name: %s, Salary: %g\n",
dataset$Name[i],
dataset$Salary[i]
))
break
}
cat(sprintf("Name: %s, Salary: %g\n",
dataset$Name[i],
dataset$Salary[i]
))
}## Name: Bagas, Salary: 5000
## Name: Joan, Salary: 7000
## Name: Alya, Salary: 6500
## Name: Dwi, Salary: 10000
## Loop stopped! Found an employee with salary above 10,000:
## Name: Nabil, Salary: 12000
Interpretation
The code will stop the loop when it finds an employee with a salary of more than 10,000.
3.4 Continue
This section uses the continue statement in a loop to skip employee records with “Average” performance.
for (i in seq_len(nrow(dataset))) {
if (dataset$Performance[i] == "Average") next
cat(sprintf("Name: %s, Performance: %s, Salary: %g\n",
dataset$Name[i],
dataset$Performance[i],
dataset$Salary[i]
))
}## Name: Bagas, Performance: Good, Salary: 5000
## Name: Joan, Performance: Very Good, Salary: 7000
## Name: Dwi, Performance: Good, Salary: 10000
## Name: Nabil, Performance: Very Good, Salary: 12000
Interpretation
The code displays the names, performance ratings, and salaries of employees, except for those with an “Average” performance rating.