Syntax and Control Flow
Week 4
INSTITUT TEKNOLOGI SAINS BANDUNG
Name : Dhefio Alim Muzakki
Student ID : 52250014
Major : Data Science
Lecturer : Mr. Bakti Siregar, M.Sc., CDS.
library(tidyverse)
library(readr)
library(ggplot2)
library(dplyr)
library(ggridges)
library(knitr)
library(DT)Introduction
Conditional statements and loops are basic concepts in programming. Conditional statements help a program make decisions based on certain conditions, while loops allow commands to be repeated automatically.
These concepts are useful in data analysis because they help process data efficiently. In this practicum, conditional statements and loops are applied using the R programming language with an employee dataset.
Conditional statements are used to calculate employee bonuses based on performance, and loops are used to display and analyze the data. This practicum helps students understand the basic use of if–else statements, for loops, and while loops in programming.
Objective
- Understand and implement conditional statements.
- Apply loops to analyze a dataset.
Conditional Statements
Conditional statements are used to make decisions in programming. In this practicum, conditional statements are used to determine employee bonuses based on their performance.
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")
)
knitr::kable(employees, caption = "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 |
Bonus Calculation
Employee bonuses are calculated based on performance levels:
- Very Good : 20% of salary
- Good : 10% of salary
- Average : 5% of salary
calculate_bonus <- function(performance, salary) {
if (performance == "Very Good") {
bonus <- salary * 0.20
} else if (performance == "Good") {
bonus <- salary * 0.10
} else {
bonus <- salary * 0.05
}
return(bonus)
}
employees$Bonus <- mapply(calculate_bonus,
employees$Performance,
employees$Salary)
knitr::kable(employees, caption = "Employee Dataset with Bonus")| ID | Name | Age | Salary | Position | Performance | Bonus |
|---|---|---|---|---|---|---|
| 1 | Bagas | 25 | 5000 | Staff | Good | 500 |
| 2 | Joan | 30 | 7000 | Supervisor | Very Good | 1400 |
| 3 | Alya | 27 | 6500 | Staff | Average | 325 |
| 4 | Dwi | 35 | 10000 | Manager | Good | 1000 |
| 5 | Nabil | 40 | 12000 | Director | Very Good | 2400 |
employees$Total <- employees$Salary + employees$Bonus
total_table <- employees[, c("Name","Salary","Bonus","Total")]
knitr::kable(total_table, caption = "Total Salary and Bonus of Employees")| Name | Salary | Bonus | Total |
|---|---|---|---|
| Bagas | 5000 | 500 | 5500 |
| Joan | 7000 | 1400 | 8400 |
| Alya | 6500 | 325 | 6825 |
| Dwi | 10000 | 1000 | 11000 |
| Nabil | 12000 | 2400 | 14400 |
Interpretation
The results show that the bonus amount depends on employee performance. Employees with Very Good performance receive the highest bonus, followed by Good, while Average performance receives the smallest bonus.
Loops (For & While)
Loops are used to repeat commands automatically. In this practicum, loops are used to iterate through the employee dataset and display specific information.
1. For Loop
The for loop is used to iterate through each row of the dataset. In this example, the loop checks which employees have a salary greater than 6000 and displays their names and salaries.
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
The for loop checks each employee in the dataset. It displays only employees whose salary is greater than 6000, which helps filter data based on a specific condition.
2. While Loop
The while loop repeats a command as long as a condition is true. In this example, the program displays employee data until a manager position is found.
i <- 1
while(i <= nrow(employees)){
cat("Name:", employees$Name[i],
", Position:", employees$Position[i], "\n")
if(employees$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)
Interpretation
The while loop displays employee information one by one until a manager position is found. When the manager appears, the loop stops automatically.
3. Break Example
Use break to stop the loop when an employee with salary
above 10000 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
The break statement stops the loop when a specific condition is met. In this example, the loop stops when an employee with a salary greater than 10000 is found.
4. Continue Example
Use continue (next in R) to skip employees
with Average performance.
for(i in 1:nrow(employees)){
if(employees$Performance[i] == "Average"){
cat("(", employees$Name[i],
"is skipped because the performance is Average )\n")
next
}
cat("Name:", employees$Name[i],
", Performance:", employees$Performance[i], "\n")
}## Name: Bagas , Performance: Good
## Name: Joan , Performance: Very Good
## ( Alya is skipped because the performance is Average )
## Name: Dwi , Performance: Good
## Name: Nabil , Performance: Very Good
Interpretation
The next statement skips certain data during the loop. In this example, employees with Average performance are skipped and not displayed.
Conclusion
This practicum demonstrates the use of conditional statements and loops in R programming. Conditional statements are used to determine employee bonuses based on their performance levels. Meanwhile, loops such as for loops and while loops help iterate through the dataset to display and analyze employee information.
By applying these concepts, datasets can be processed more efficiently and repetitive tasks can be automated in data analysis.