Tugas Week 4 ~ Syntax and Control Flow
Fifi Muthia Pitaloka
NIM: 52250038
Dosen Pengampu: Bakti Siregar, M.Sc., CDS.
Mata Kuliah: Pemrograman Sains Data I
Program Studi: Sains Data
Institut Teknologi Sains Bandung
Introduction
This practicum aims to explore the basic syntax and control flow in the R programming language. Control flow manages program execution based on specific conditions, such as conditional statements (if, else if, else) and loops (for loop and while loop). The dataset employed is a simple employee dataset containing details like name, salary, position, and work performance. Through this dataset, the program performs tasks including calculating employee bonuses, displaying data under certain conditions, terminating processes with break, and skipping non-qualifying data using next. These concepts serve as a crucial foundation for data processing and analysis in R.
Dataset
This section utilizes an employee dataset containing basic information such as ID, name, age, salary, position, and work performance. The dataset is loaded for analysis using various programming structures. It serves as the foundation for subsequent operations, including retrieving specific information, applying conditional statements, and employing loops.
employee_data <- read.csv("C:/Users/FIFI/Desktop/tugas kuliah/tugas pemrograman sem 2/tugas week 4/employees_dataset.csv")
names(employee_data)[names(employee_data) == "Performance."] <- "Performance"
knitr::kable(employee_data)
| 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 |
name <- employee_data$Name
salary <- employee_data$Salary
performance <- employee_data$Performance
position <- employee_data$Position
Conditional Statements
In this section, conditional statements determine employee bonuses based on work performance. The program checks each employee’s performance value and assigns the bonus percentage according to these criteria:
- If performance is “Very Good”: 20% salary bonus
- If performance is “Good”: 10% salary bonus
- Otherwise: 5% salary bonus
The calculated bonus results are then displayed for each employee.
bonus <- c()
for (i in 1:length(name)) {
if (performance[i] == "Very Good") {
b <- salary[i] * 0.20
} else if (performance[i] == "Good") {
b <- salary[i] * 0.10
} else {
b <- salary[i] * 0.05
}
bonus[i] <- b
cat("Name:", name[i], "- Bonus:", b, "\n")
}
## Name: Bagas - Bonus: 500
## Name: Joan - Bonus: 1400
## Name: Alya - Bonus: 325
## Name: Dwi - Bonus: 1000
## Name: Nabil - Bonus: 2400
bonus_table <- data.frame(
Name = name,
Salary = salary,
Performance = performance,
Bonus = bonus
)
knitr::kable(bonus_table)
| Name | Salary | Performance | Bonus |
|---|---|---|---|
| Bagas | 5000 | Good | 500 |
| Joan | 7000 | Very Good | 1400 |
| Alya | 6500 | Average | 325 |
| Dwi | 10000 | Good | 1000 |
| Nabil | 12000 | Very Good | 2400 |
Interpretation
The calculation results show that each employee’s bonus varies according to their work performance level. Those with “Very Good” performance receive a 20% salary bonus, “Good” get 10%, and “Average” 5%. It’s clear that employees with higher salaries and better performance earn larger bonuses. This system takes into account both individual performance and salary levels.
Loops
This section uses loop structures to process employee data iteratively. The two types are for loop and while loop. This enables the program to examine each dataset entry and display information for employees with salaries above 6000.
For Loop
This section applies a for loop to iteratively process employee data in the dataset. The program examines each entry and displays employees with salaries above 6000. A for loop ensures systematic data filtering across all records.
filtered_name <- c()
filtered_salary <- c()
for (i in 1:length(name)) {
if (salary[i] > 6000) {
cat("Name:", name[i], "- Salary:", salary[i], "\n")
filtered_name <- c(filtered_name, name[i])
filtered_salary <- c(filtered_salary, salary[i])
}
}
## Name: Joan - Salary: 7000
## Name: Alya - Salary: 6500
## Name: Dwi - Salary: 10000
## Name: Nabil - Salary: 12000
salary_table <- data.frame(
Name = filtered_name,
Salary = filtered_salary
)
knitr::kable(salary_table)
| Name | Salary |
|---|---|
| Joan | 7000 |
| Alya | 6500 |
| Dwi | 10000 |
| Nabil | 12000 |
Interpretation
Using a for loop, the program filtered employees with salaries above 6000: Joan, Alya, Dwi, and Nabil. The loop examines data sequentially and displays only those meeting the condition. This shows a for loop is highly effective for processing entire datasets systematically.
While Loop
This section implements a while loop to display employee data sequentially until a “Manager” position is found. The program reads data one by one and stops when the condition is met. This illustrates how a while loop controls the process based on specific conditions.
i <- 1
manager_name <- c()
manager_position <- c()
while (i <= length(name)) {
cat("Name:", name[i], "- Position:", position[i], "\n")
manager_name <- c(manager_name, name[i])
manager_position <- c(manager_position, position[i])
if (position[i] == "Manager") {
cat("(Stop here)\n")
break
}
i <- i + 1
}
## Name: Bagas - Position: Staff
## Name: Joan - Position: Supervisor
## Name: Alya - Position: Staff
## Name: Dwi - Position: Manager
## (Stop here)
manager_table <- data.frame(
Name = manager_name,
Position = manager_position
)
knitr::kable(manager_table)
| Name | Position |
|---|---|
| Bagas | Staff |
| Joan | Supervisor |
| Alya | Staff |
| Dwi | Manager |
Interpretation
The result shows the program displaying employee data sequentially until a “Manager” position is found. The process stops using the break command once that condition is met. This proves a while loop is effective for controlling data searches and halting iteration appropriately.
Break Statement
This section uses the break command to stop the loop when a specific condition is met. The program examines employee data sequentially and displays salary information. If a salary exceeds 10000, the loop terminates immediately.
break_name <- c()
break_salary <- c()
for (i in 1:length(name)) {
if (salary[i] > 10000) {
cat("(Stopped because", name[i], "has a salary above 10000)\n")
break
}
cat("Name:", name[i], "- Salary:", salary[i], "\n")
break_name <- c(break_name, name[i])
break_salary <- c(break_salary, 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 10000)
break_table <- data.frame(
Name = break_name,
Salary = break_salary
)
knitr::kable(break_table)
| Name | Salary |
|---|---|
| Bagas | 5000 |
| Joan | 7000 |
| Alya | 6500 |
| Dwi | 10000 |
Interpretation
The program results confirm that break stops the loop when the condition is met. The loop halts at employee Nabil with salary above 10000. Only data up to that point is displayed.
Continue Statement
This section introduces the continue concept in loops to skip specific data. In R, the next command bypasses the current iteration and proceeds to the next. For example, the program skips employees with “Average” performance.
continue_name <- c()
continue_performance <- c()
skip_message <- ""
for (i in 1:length(name)) {
if (performance[i] == "Average") {
skip_message <- paste(name[i], "is skipped because the performance is 'Average'")
next
}
cat("Name:", name[i], "- Performance:", performance[i], "\n")
continue_name <- c(continue_name, name[i])
continue_performance <- c(continue_performance, performance[i])
}
## Name: Bagas - Performance: Good
## Name: Joan - Performance: Very Good
## Name: Dwi - Performance: Good
## Name: Nabil - Performance: Very Good
continue_table <- data.frame(
Name = continue_name,
Performance = continue_performance
)
knitr::kable(continue_table)
| Name | Performance |
|---|---|
| Bagas | Good |
| Joan | Very Good |
| Dwi | Good |
| Nabil | Very Good |
Interpretation
The program results prove that next skips data meeting certain conditions in the loop. “Average” performance employees are bypassed, so they don’t appear in the output or results table. This highlights the continue function for ignoring non-matching data.
Conclusion
From this practicum, control structures like conditional statements, loops, break, and continue greatly aid systematic data processing. Conditional statements handle decisions based on conditions, such as calculating bonuses from performance. For and while loops process data repeatedly. Break stops loops at key conditions, while continue (next in R) skips unfit data. Mastering these makes data analysis more efficient and structured.
Reference
[1] 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