Prakticum Week 4 ~ Data Science Programming

Lulu Najla Salsabila

INSTITUT TEKNOLOGI SAINS BANDUNG

1 Introduction

Programming is an important skill in data science because it is used to process, analyze, and manage data efficiently.In programming, there are basic concepts such as conditional statements and loops that help control the flow of a program. Conditional statements are used to make decisions based on certain conditions, while loops are used to repeat a process multiple times. In this task, Python and R are used to practice these concepts by analyzing a simple employee dataset, such as calculating bonuses, displaying employee data based on certain criteria, and controlling loops using commands like break and continue.

2 Dataset

# Memanggil library untuk tabel
library(knitr)

# Membuat dataset karyawan langsung dalam data frame
employee <- 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")
)

# Menampilkan tabel
kable(employee, caption = "Employee Dataset")
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

3 Conditional statements

Determine bonus lavels based on employee performance:

  • Very Good -> 20% Of salary
  • Good -> 10% Of salary
  • Average -> 5% Of Salary
# Membuat dataset karyawan dalam bentuk data frame
nama <- c("Bagas","Joan","Alya","Dwi","Nabil")
usia <- c(25,30,27,35,40)
gaji <- c(5000,7000,6500,10000,12000)
posisi <- c("Staf","Pengawas","Staf","Manajer","Direktur")
kinerja <- c("Bagus","Sangat bagus","Rata-rata","Bagus","Sangat bagus")

data <- data.frame(nama, usia, gaji, posisi, kinerja)

# Perulangan untuk menghitung bonus tiap karyawan
for(i in 1:nrow(data)){

  # Mengambil gaji dan kinerja
  gaji_karyawan <- data$gaji[i]
  kinerja_karyawan <- data$kinerja[i]

  # Menggunakan kondisi if else untuk menentukan bonus
  if(kinerja_karyawan == "Sangat bagus"){
    bonus <- gaji_karyawan * 0.20
  }
  else if(kinerja_karyawan == "Bagus"){
    bonus <- gaji_karyawan * 0.10
  }
  else{
    bonus <- gaji_karyawan * 0.05
  }

  # Menampilkan hasil
  cat("Name:", data$nama[i], ", Bonus:", bonus, "\n")
}
## Name: Bagas , Bonus: 500 
## Name: Joan , Bonus: 1400 
## Name: Alya , Bonus: 325 
## Name: Dwi , Bonus: 1000 
## Name: Nabil , Bonus: 2400
# Memanggil library untuk membuat tabel
library(knitr)

# Membuat dataset karyawan
nama <- c("Bagas","Joan","Alya","Dwi","Nabil")
usia <- c(25,30,27,35,40)
gaji <- c(5000,7000,6500,10000,12000)
posisi <- c("Staf","Pengawas","Staf","Manajer","Direktur")
kinerja <- c("Bagus","Sangat bagus","Rata-rata","Bagus","Sangat bagus")

data <- data.frame(nama, usia, gaji, posisi, kinerja)

# Membuat kolom bonus kosong
bonus <- c()

# Perulangan untuk menghitung bonus
for(i in 1:nrow(data)){

  gaji_karyawan <- data$gaji[i]
  kinerja_karyawan <- data$kinerja[i]

  if(kinerja_karyawan == "Sangat bagus"){
    bonus[i] <- gaji_karyawan * 0.20
  }
  else if(kinerja_karyawan == "Bagus"){
    bonus[i] <- gaji_karyawan * 0.10
  }
  else{
    bonus[i] <- gaji_karyawan * 0.05
  }
}

# Membuat tabel hasil hanya nama dan bonus
hasil <- data.frame(
  Name = data$nama,
  Bonus = bonus
)

# Menampilkan tabel
kable(hasil, caption = "Employee Bonus")
Employee Bonus
Name Bonus
Bagas 500
Joan 1400
Alya 325
Dwi 1000
Nabil 2400

3.1 Interpretation

Based on the result table, employee bonuses are determined by their performance level. Employees with “Very Good” performance receive a 20% bonus of their salary, employees with “Good” performance receive 10%, and employees with “Average” performance receive 5% of their salary. The results show that employees with very good performance, such as Joan and Nabil, receive higher bonuses compared to the others.

4 Loops (for & While)

  1. Use a for loop to list employees with a salary greater than 6000.
# Membuat dataset
nama <- c("Bagas","Joan","Alya","Dwi","Nabil")
gaji <- c(5000,7000,6500,10000,12000)

data <- data.frame(nama,gaji)

# Perulangan for
for(i in 1:nrow(data)){

  # Mengecek apakah gaji lebih dari 6000
  if(data$gaji[i] > 6000){

    # Menampilkan hasil
    cat("Name:", data$nama[i], ", Salary:", data$gaji[i], "\n")

  }

}
## Name: Joan , Salary: 7000 
## Name: Alya , Salary: 6500 
## Name: Dwi , Salary: 10000 
## Name: Nabil , Salary: 12000

Interpretation

The result shows employees whose salaries are greater than 6000. From the data, Joan, Alya, Dwi, and Nabil fall into the category of employees with salaries above 6000, so they are included in the results. Meanwhile, Bagas is not included because the salary is below the specified limit. This indicates that the data is filtered based on a specific salary criterion.

  1. Use a while loop to display employees until a “meneger” is found.
# Dataset
nama <- c("Bagas","Joan","Alya","Dwi","Nabil")
posisi <- c("Staff","Supervisor","Staff","Manajer","Direktur")

data <- data.frame(nama,posisi)

# index awal
i <- 1

# perulangan while
while(i <= nrow(data)){

  # menampilkan nama dan posisi
  cat("Name:", data$nama[i], ", Position:", data$posisi[i], "\n")

  # jika posisi Manajer maka berhenti
  if(data$posisi[i] == "Manajer"){
    cat("(Stop here)\n")
    break
  }

  # menambah index
  i <- i + 1

}
## Name: Bagas , Position: Staff 
## Name: Joan , Position: Supervisor 
## Name: Alya , Position: Staff 
## Name: Dwi , Position: Manajer 
## (Stop here)

Interpretation

In this process, a while loop is used to display employee data sequentially based on their name and position. The data continues to be displayed as long as the loop condition is satisfied. However, when an employee with the position of Manager, namely Dwi, is found, the loop stops. As a result, the remaining data after the Manager position is not displayed. This shows that a loop can be controlled to stop when a specific condition is met.

  1. Use break to stop the loop when an employee with a salary above 10.000 is found.
# Create data
name <- c("Bagas", "Joan", "Alya", "Dwi", "Nabil")
salary <- c(5000, 7000, 6500, 10000, 12000)

# Loop through employees
for (i in 1:length(name)) {
  
  # Stop if salary is above 10000
  if (salary[i] > 10000) {
    cat("(Stopped because Nabil has a salary above 10,000)\n")
    break
  }
  
  # Display name and salary
  cat("Name:", name[i], ", Salary:", 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

This program displays the employees’ names and salaries from the dataset. When the program finds an employee with a salary above 10,000, the break command immediately stops the process. Since Nabil has a salary above 10,000, the program stops after showing Dwi’s data, so Nabil’s data is not displayed.

  1. Use continue to skip employees with “average” performance.
# Create data
name <- c("Bagas", "Joan", "Alya", "Dwi", "Nabil")
performance <- c("Good", "Very Good", "Average", "Good", "Very Good")

skipped <- ""

for (i in 1:length(name)) {
  
  if (performance[i] == "Average") {
    skipped <- name[i]
    next
  }
  
  cat("Name:", name[i], ", Performance:", performance[i], "\n")
}
## Name: Bagas , Performance: Good 
## Name: Joan , Performance: Very Good 
## Name: Dwi , Performance: Good 
## Name: Nabil , Performance: Very Good
cat("(", skipped, "is skipped because the performance is \"Average\")")
## ( Alya is skipped because the performance is "Average")

Interpretation

In this data, employees have different performance ratings. The employee with “Average” performance is skipped using the next command. Because of that, Alya is not included and the process continues to the next employee.

5 conclusion

In conclusion, conditional statements and loops are fundamental concepts in programming that help process and analyze data more efficiently. By using conditions such as if, elif, and else, a program can make decisions based on specific criteria. Meanwhile, loops like for and while allow repetitive data processing. Commands such as break and continue are also useful to control the flow of loops. Through this exercise, the basic logic of programming in Python and R can be applied to analyze simple datasets in a more systematic way.

6 References

Siregar, B. (2025) Syntax and Control Flow. In Data Science Programming. Data Science Labs.

https://bookdown.org/dsciencelabs/data_science_programming/02-Syntax-and-Control-Flow.html