Clara Maisie Wanghili
52250039
Student Major in Data Science at
Institut Teknologi Sains Bandung
Introduction
Programming can be used to read and process data from a dataset. By using loops and conditional statements, a program can go through the data one by one and perform different actions depending on certain conditions.
Structures like for loops, while loops, break, and continue help control how the program runs when working with data. With these basic concepts, data can be filtered, calculated, and displayed more efficiently.
1 Load Dataset
Dummy Dataset
# load dataset
data <- read.csv("dataset.csv")
# show dataset
data
Conditional Statement
Bonus calculation
# calculate bonus based on performance
data$Bonus <- 0
for(i in 1:nrow(data)){
if(data$Performance[i] == "Very Good"){
data$Bonus[i] <- data$Salary[i] * 0.20
} else if(data$Performance[i] == "Good"){
data$Bonus[i] <- data$Salary[i] * 0.10
} else {
data$Bonus[i] <- data$Salary[i] * 0.05
}
}
# table
data
The table shows the employee data along with the bonus that is calculated based on their performance. The program uses a for loop and if-else conditions to check each person’s performance and determine the bonus amount.
Employees with Very Good performance receive a 20% bonus, employees with Good performance receive 10%, and employees with Average performance receive 5% of their salary. The result is displayed in a table that includes the employee’s name, salary, performance, and the calculated bonus.
3 Loops (For & While Loop, Break, Continue/Next)
3.1 For Loop
# show employees with salary greater than 6000
table <- data.frame(Name=character(),
Salary=numeric())
for(i in 1:nrow(data)){
if(data$Salary[i] > 6000){
table <- rbind(table,
data.frame(Name=data$Name[i],
Salary=data$Salary[i]))
}
}
# show table
table
In this part, the program uses a for loop to check each row in the dataset. The program looks at the salary value and checks if it is greater than 6000.
If the condition is true, the program adds that data to the result table. From the output, we can see that Joan, Alya, Dwi, and Nabil have salaries above 6000, so their names and salaries appear in the table.
3.2 While Loop
# display data until manager is found
table <- data.frame(Name=character(),
Position=character())
i <- 1
while(i <= nrow(data)){
table <- rbind(table,
data.frame(Name=data$Name[i],
Position=data$Position[i]))
if(data$Position[i] == "Manager"){
break
}
i <- i + 1
}
# table
table
In this part, the program uses a while loop to go through the dataset one by one. It shows the name and position of each person.
The loop continues until it finds someone with the position Manager. When the program finds Dwi, the loop stops and the data before that is shown in the table.
3.3 Break
# stop loop when salary above 10000
table <- data.frame(Name=character(),
Salary=numeric())
for(i in 1:nrow(data)){
if(data$Salary[i] > 10000){
break
}
table <- rbind(table,
data.frame(Name=data$Name[i],
Salary=data$Salary[i]))
}
# table
table
In this part, the program uses a for loop to go through the dataset and show the name and salary. The loop continues normally until it finds a salary above 10000.
When the program finds that condition, it uses break to stop the loop immediately. Because of that, the program does not continue to the next data after that point.
3.4 Continue/Next
# skip employees with average performance
table <- data.frame(Name=character(),
Performance=character())
for(i in 1:nrow(data)){
if(data$Performance[i] == "Average"){
next
}
table <- rbind(table,
data.frame(Name=data$Name[i],
Performance=data$Performance[i]))
}
# table
table
In this part, the program uses a for loop to check the performance of each person in the dataset. If the performance is Average, the program uses next to skip that data. Because of that, the person with Average performance is not included in the result table.
Conclusion
From this program, we can see how loops and conditions are used to read and process data in a dataset. The program checks the data one by one and does several things, such as calculating bonuses, showing certain data, stopping the loop, or skipping some data. This way, the data can be processed and displayed based on the conditions that were given.