Practicum Syntax & Control Flow

Practicum ~week 04~

Khafizatun Nisa
Data Science Undergraduate Student

R Programming Data Science Statistics

Dataset

The dataset used in this study contains employee information including their name, age, salary, position, and performance evaluation.This dataset will be used to demonstrate several programming concepts such as conditional statements, loops, and control flow in R.

library(knitr)
library(kableExtra)

# create dataset
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")

employees <- data.frame(name,age,salary,position,performance)

# show dataset table
employees %>%
kable(
  col.names = c("Name","Age","Salary","Position","Performance"),
  caption = "Employee Dataset"
) %>%
kable_styling(
  bootstrap_options = c("striped","hover","responsive"),
  full_width = TRUE
) %>%
row_spec(0, bold = TRUE, color = "white", background = "#7C1D3D")
Employee Dataset
Name Age Salary Position Performance
Bagas 25 5000 Staff Good
Joan 30 7000 Supervisor Very Good
Alya 27 6500 Staff Average
Dwi 35 10000 Manager Good
Nabil 40 12000 Director Very Good

Task 1 – Conditional Statement

This task demonstrates the use of a conditional statement (if–else) to determine employee bonuses based on their performance evaluation. 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.

library(dplyr)
library(knitr)
library(kableExtra)

# calculate bonus
employees$bonus <- ifelse(employees$performance == "Very Good", employees$salary * 0.20,
                   ifelse(employees$performance == "Good", employees$salary * 0.10,
                          employees$salary * 0.05))

# show table
employees %>%
select(name, performance, salary, bonus) %>%
kable(
  col.names = c("Employee Name","Performance","Salary","Bonus"),
  caption = "Employee Bonus Calculation"
) %>%
kable_styling(
  bootstrap_options = c("striped","hover","responsive"),
  full_width = TRUE
) %>%
row_spec(0, bold = TRUE, color = "white", background = "#7C1D3D")
Employee Bonus Calculation
Employee Name Performance Salary Bonus
Bagas Good 5000 500
Joan Very Good 7000 1400
Alya Average 6500 325
Dwi Good 10000 1000
Nabil Very Good 12000 2400
library(ggplot2)

ggplot(employees, aes(x = name, y = bonus, fill = performance)) +
  geom_bar(stat = "identity") +
  labs(
    title = "Employee Bonus Distribution Based on Performance",
    x = "Employee Name",
    y = "Bonus"
  ) +
  theme_minimal() +
  scale_fill_brewer(palette = "Reds")

Interpretation

Based on the table and bar chart above, employee bonuses vary depending on their performance level. Employees with Very Good performance receive the highest bonuses, followed by employees with Good performance, while employees with Average performance receive the lowest bonuses.

The visualization further highlights this difference by clearly showing how performance evaluation directly influences the bonus distribution among employees.

Task 2 – For Loop

This task demonstrates the use of a for loop to iterate through employee data and display their information such as name, age, and position.

library(knitr)
library(kableExtra)

# create empty vectors
name_list <- c()
age_list <- c()
position_list <- c()

# loop through employees data
for(i in 1:nrow(employees)){
  
  name_list[i] <- employees$name[i]
  age_list[i] <- employees$age[i]
  position_list[i] <- employees$position[i]
  
}

# create new table
employee_info <- data.frame(
  Name = name_list,
  Age = age_list,
  Position = position_list
)

# display table
employee_info %>%
kable(
  caption = "Employee Information Using For Loop"
) %>%
kable_styling(
  bootstrap_options = c("striped","hover","responsive"),
  full_width = TRUE
) %>%
row_spec(0, bold = TRUE, color = "white", background = "#7C1D3D")
Employee Information Using For Loop
Name Age Position
Bagas 25 Staff
Joan 30 Supervisor
Alya 27 Staff
Dwi 35 Manager
Nabil 40 Director

Interpretation The result demonstrates how a for loop can iterate through each row of the dataset and extract specific information such as employee name, age, and position.

By looping through the dataset, the program systematically processes each employee record and stores the selected attributes into a new table. This illustrates how loops can be used to efficiently access and organize structured data in programming.

Task 3 – Next Statement

This task demonstrates the use of the next statement in a loop.The next statement is used to skip certain iterations in a loop when a specific condition is met. In this example, employees with Average performance will be skipped, so only employees with Good and Very Good performance will be displayed.

library(knitr)
library(kableExtra)

# create empty data frame
filtered_employees <- data.frame()

# loop through employees
for(i in 1:nrow(employees)){
  
  # skip employees with Average performance
  if(employees$performance[i] == "Average"){
    next
  }
  
  # add remaining employees
  filtered_employees <- rbind(filtered_employees, employees[i,])
}

# show table
filtered_employees %>%
kable(
  caption = "Employees with Good and Very Good Performance"
) %>%
kable_styling(
  bootstrap_options = c("striped","hover","responsive"),
  full_width = TRUE
) %>%
row_spec(0, bold = TRUE, color = "white", background = "#7C1D3D")
Employees with Good and Very Good Performance
name age salary position performance bonus
1 Bagas 25 5000 Staff Good 500
2 Joan 30 7000 Supervisor Very Good 1400
4 Dwi 35 10000 Manager Good 1000
5 Nabil 40 12000 Director Very Good 2400

Interpretation

The result shows that employees with Average performance are excluded from the table.
This demonstrates how the next statement allows the loop to skip specific iterations without stopping the entire loop process.

As a result, only employees with Good and Very Good performance are included in the final output.

Task 4 – Break Statement

The break statement is used to stop a loop immediately when a specific condition is met.
In this task, the loop iterates through employee data but will stop when a salary greater than 5000 is encountered.

result <- data.frame(
  name = character(),
  salary = numeric()
)

for(i in 1:nrow(employees)){

  if(employees$salary[i] > 5000){
    break
  }

  result <- rbind(
    result,
    data.frame(
      name = employees$name[i],
      salary = employees$salary[i]
    )
  )
}

result %>%
kable(
  col.names = c("Name","Salary"),
  caption = "Employees Processed Before Break Condition"
) %>%
kable_styling(
  bootstrap_options = c("striped","hover","responsive"),
  full_width = TRUE
) %>%
row_spec(0, bold = TRUE, color = "white", background = "#7C1D3D")
Employees Processed Before Break Condition
Name Salary
Bagas 5000

Interpretation
The result shows that the loop processes employee data only until the first salary greater than 5000 appears.Once this condition is met, the break statement immediately stops the loop, preventing further iterations.This demonstrates how break can control program execution and improve efficiency by terminating loops when a specific condition is reached.

Task 5 – While Loop

This task demonstrates the use of a while loop to iterate through employee data.
A while loop repeatedly executes a block of code as long as a specified condition remains true. In this example, the loop processes employee information sequentially until all records in the dataset have been accessed.

i <- 1

result <- data.frame(
  name = character(),
  performance = character()
)

while(i <= nrow(employees)){

  result <- rbind(
    result,
    data.frame(
      name = employees$name[i],
      performance = employees$performance[i]
    )
  )

  i <- i + 1
}

result %>%
kable(
  col.names = c("Employee Name","Performance"),
  caption = "Employee Performance Using While Loop"
) %>%
kable_styling(
  bootstrap_options = c("striped","hover","responsive"),
  full_width = TRUE
) %>%
row_spec(0, bold = TRUE, color = "white", background = "#7C1D3D")
Employee Performance Using While Loop
Employee Name Performance
Bagas Good
Joan Very Good
Alya Average
Dwi Good
Nabil Very Good

Interpretation

The output table shows the performance evaluation of each employee processed using a while loop. The loop starts from the first row of the dataset and continues until all employee records have been iterated.

This example demonstrates how while loops can be used to control iteration based on a condition, making them useful for sequential data processing tasks.

Conclusion

This practicum demonstrates several fundamental programming concepts in R, including conditional statements, loops, and control flow mechanisms. Through the employee dataset, different programming structures such as if–else conditions, for loops, next statements, break statements, and while loops were implemented to process and analyze structured data.

The tasks illustrate how programming logic can be used to automate repetitive operations, filter data, control iteration processes, and generate meaningful outputs such as tables and visualizations. These techniques are essential in data science programming because they allow programmers to efficiently manipulate datasets and extract useful insights.

Overall, this practicum helps strengthen the understanding of syntax and control flow in R, which are fundamental skills required for performing data processing and analytical tasks in the field of data science.