Practicum
Assignment Week 4
Angelica Florentina M.
52250063
Student Majoring in Data Science at Institut Teknologi Sains Bandung
Introduction
This report discusses the processing of an employee dataset using the R programming language in RStudio. The dataset contains various employee information, such as name, age, salary, job title, and work performance. The data is then analyzed using basic programming concepts such as conditional statements and looping structures. The objective of this assignment is to understand and apply conditional statements and looping techniques (for loops, while loops, break and continue) to analyze a dataset.
Dataset
# Membuat dataset
df <- data.frame(
Id = c(1,2,3,4,5),
Name = c("Bagus","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 data
df1. Conditional Statements
# Menentukan bonus berdasarkan kinerja karyawan
df$Bonus <- ifelse(df$Performance == "Very Good", df$Salary * 0.20,
ifelse(df$Performance == "Good", df$Salary * 0.10,
ifelse(df$Performance == "Average", df$Salary * 0.05, 0)))
# Menampilkan data
df[, c("Id", "Name", "Salary", "Performance", "Bonus")]2. For Loop
# Salary > 6000
for(i in 1:nrow(df)){
if(df$Salary[i] > 6000){
cat("Name:", df$Name[i], ", Salary:", df$Salary[i], "\n")
}
}## Name: Joan , Salary: 7000
## Name: Alya , Salary: 6500
## Name: Dwi , Salary: 10000
## Name: Nabil , Salary: 12000
3. While Loop
i <- 1
while(i <= nrow(df)){
if(df$Position[i] == "Manager"){
cat("Name:", df$Name[i], ", Position:", df$Position[i], "(Stop here)\n")
break
} else {
cat("Name:", df$Name[i], ", Position:", df$Position[i], "\n")
}
i <- i + 1
}## Name: Bagus , Position: Staff
## Name: Joan , Position: Supervisor
## Name: Alya , Position: Staff
## Name: Dwi , Position: Manager (Stop here)
4. Break
# Break saat salary > 10000
for(i in 1:nrow(df)){
if(df$Salary[i] > 10000){
cat("Stopped because Nabil has a salary above 10,000\n")
break
}
cat("Name:", df$Name[i], ", Salary:", df$Salary[i], "\n")
}## Name: Bagus , Salary: 5000
## Name: Joan , Salary: 7000
## Name: Alya , Salary: 6500
## Name: Dwi , Salary: 10000
## Stopped because Nabil has a salary above 10,000
5. Continue atau Next
# Continue untuk skip average
for(i in 1:nrow(df)){
if(df$Performance[i] == "Average"){
cat('Alya is skipped because the performance is "Average"\n')
next
}
cat("Name:", df$Name[i], ", Performance:", df$Performance[i], "\n")
}## Name: Bagus , Performance: Good
## Name: Joan , Performance: Very Good
## Alya is skipped because the performance is "Average"
## Name: Dwi , Performance: Good
## Name: Nabil , Performance: Very Good
Conclusion
Based on the analysis, the use of conditional statements and looping structures in R can assist in processing and displaying data according to desired conditions. By utilizing for loops, while loops, and controls such as break and next, the dataset analysis process can be performed more systematically and efficiently.
References
R for Data Science - Wickham, H., & Grolemund, G. (2017). R for Data Science: Import, Tidy, Transform, Visualize, and Model Data. O’Reilly Media.
R Programming for Data Science - Peng, R. D. (2016). R Programming for Data Science. Leanpub.
https://bookdown.org/dsciencelabs/data_science_programming/02-Syntax-and-Control-Flow.html