Dhea Putri Khasanah

Data Science Student at ITSB

R Programming Data Science Python

📍 Institut Teknologi Sains Bandung

Lecturer In Charge

Bakti Siregar, M.Sc., CDS

Mata Kuliah: Programming for Data Science 1


1 Introduction

This practicum demonstrates the use of conditional statements and control flow in programming.
The analysis uses a small employee dataset to calculate bonuses, filter salaries, and demonstrate loop control such as for, while, break, and continue.

2 Process

2.1 Dataset

employees <- list(
  list(name = "Bagas", age = 25, salary =  5000, position = "Staff",      performance = "Good"),
  list(name = "Joan",  age = 30, salary =  7000, position = "Supervisor", performance = "Very Good"),
  list(name = "Alya",  age = 27, salary =  6500, position = "Staff",      performance = "Average"),
  list(name = "Dwi",   age = 35, salary = 10000, position = "Manager",    performance = "Good"),
  list(name = "Nabil", age = 40, salary = 12000, position = "Director",   performance = "Very Good")
)
Table 2.1: EMPLOYEE DATA REPORTS
Name Age Salary Position Performance
Bagas 25 5,000 Staff Good
Joan 30 7,000 Supervisor Very Good
Alya 27 6,500 Staff Average
Dwi 35 10,000 Manager Good
Nabil 40 12,000 Director Very Good

Dataset Interpretation.

1. Workforce Overview & Demographics

  • Analysis: The dataset represents a cross-section of a corporate hierarchy, ranging from Staff to Director levels. The age distribution spans from 25 to 40 years, with an average age of approximately 31.4 years.

  • Insight: This indicates a relatively young and productive workforce, where career progression (from Staff to Management) is visible as age and experience increase.

2. Compensation Hierarchy

  • Analysis: There is a clear positive correlation between Position and Salary. Compensation starts at 5,000 for entry-level Staff and peaks at 12,000 for the Director position.

  • Insight: The salary structure is well-defined and follows a standard corporate grading system. The significant gap between the Manager (\(10,000\)) and Director (\(12,000\)) levels reflects the high level of responsibility and strategic decision-making required at the top of the organization.

3. Performance Distribution

  • Analysis: Performance metrics are varied, with 40% of employees achieving a “Very Good” rating, 40% at “Good”, and 20% at “Average”.

  • Insight: The high percentage of “Very Good” and “Good” ratings (80% combined) suggests a high-performing culture. However, the presence of an “Average” rating for a Staff position (Alya) indicates a potential area for targeted training or performance improvement programs.

4. Strategic Relationship (Salary vs. Performance)

  • Analysis: It is noteworthy that higher-salaried roles (Joan and Nabil) are also the top performers (“Very Good”).

  • Business Insight: This alignment suggests that the company successfully incentivizes top talent. High earners are delivering high value, which justifies the company’s investment in senior personnel and leadership.

2.2 Helper Function

print_header <- function(title) {
  cat("\n", strrep("=", 60), "\n", sep = "")
  cat(format(title, width = 60, justify = "centre"), "\n")
  cat(strrep("=", 60), "\n")
}

2.3 1. Bonus Calculation

Bonus Calculation Rules:

  • Very Good -> 20% of salary
  • Good -> 10% of salary
  • Average -> 5% of salary
print_header("EMPLOYEE BONUS TABLE")
## 
## ============================================================
##                     EMPLOYEE BONUS TABLE                     
## ============================================================
cat(sprintf("%-10s %-10s %-15s %-10s\n", "Name", "Salary", "Performance", "Bonus"))
## Name       Salary     Performance     Bonus
cat(strrep("-", 60), "\n")
## ------------------------------------------------------------
for (emp in employees) {
  if (emp$performance == "Very Good") {
    bonus <- emp$salary * 0.20
  } else if (emp$performance == "Good") {
    bonus <- emp$salary * 0.10
  } else {
    bonus <- emp$salary * 0.05
  }
  cat(sprintf("%-10s %-10d %-15s %-10.0f\n",
              emp$name, emp$salary, emp$performance, bonus))
}
## Bagas      5000       Good            500       
## Joan       7000       Very Good       1400      
## Alya       6500       Average         325       
## Dwi        10000      Good            1000      
## Nabil      12000      Very Good       2400
Table 2.2: EMPLOYEE BONUS TABLE
Name Position Performance Salary Bonus Total_Pay
Bagas Staff Good 5,000 500 5,500
Joan Supervisor Very Good 7,000 1,400 8,400
Alya Staff Average 6,500 325 6,825
Dwi Manager Good 10,000 1,000 11,000
Nabil Director Very Good 12,000 2,400 14,400

2.4 2. Salary Filter

print_header("EMPLOYEES WITH SALARY > 6000")
## 
## ============================================================
##                 EMPLOYEES WITH SALARY > 6000                 
## ============================================================
cat(sprintf("%-10s %-10s\n", "Name", "Salary"))
## Name       Salary
cat(strrep("-", 25), "\n")
## -------------------------
for (emp in employees) {
  if (emp$salary > 6000) {
    cat(sprintf("%-10s %-10d\n", emp$name, emp$salary))
  }
}
## Joan       7000      
## Alya       6500      
## Dwi        10000     
## Nabil      12000

2.4.1 Employees With Salary > 6000

Table 2.3: 2.5.3.1 - EMPLOYEES WITH SALARY > 6000
Name Salary
2 Joan 7000
3 Alya 6500
4 Dwi 10000
5 Nabil 12000

Analysis of Employees with High Salary (> 6,000)

  • Logic Applied: Iterative filtering (For Loop equivalent).

  • Interpretation: This table identifies key personnel with high compensation levels. From the dataset, four employees (Joan, Alya, Dwi, and Nabil) meet the criteria.

  • Business Insight: This list helps management monitor high-tier labor costs. It shows that 80% of the sample workforce earns above the 6,000 threshold, indicating a senior-heavy or high-skill distribution within this group.


2.5 3. While Loop — Search Until Manager

print_header("SEARCH UNTIL MANAGER")
## 
## ============================================================
##                     SEARCH UNTIL MANAGER                     
## ============================================================
cat(sprintf("%-10s %-15s\n", "Name", "Position"))
## Name       Position
cat(strrep("-", 30), "\n")
## ------------------------------
i <- 1  # Di R, index dimulai dari 1
while (i <= length(employees)) {
  emp <- employees[[i]]
  cat(sprintf("%-10s %-15s\n", emp$name, emp$position))
  
  if (emp$position == "Manager") {
    cat("\nStop here (Manager Found)\n")
    break
  }
  i <- i + 1
}
## Bagas      Staff          
## Joan       Supervisor     
## Alya       Staff          
## Dwi        Manager        
## 
## Stop here (Manager Found)

2.5.1 Scanning Until Manager Found

Table 2.4: 2.5.3.2 - SCANNING UNTIL MANAGER FOUND
Name Position Note
Bagas Staff
Joan Supervisor
Alya Staff
Dwi Manager Stop here

Scanning Process (While Loop Logic)

  • Logic Applied: Conditional termination using a while loop.

  • Interpretation: The system scans the organizational hierarchy sequentially starting from the first entry. The process remains active through Bagas (Staff) and Joan (Supervisor) but terminates immediately upon reaching Dwi (Manager).

  • Business Insight: This reflects an automated search efficiency. Instead of processing the entire database, the system stops as soon as the target role is identified, saving computational resources and focusing on the relevant chain of command.


2.6 4. Break — Stop at High Salary

print_header("BREAK CONDITION (SALARY > 10000)")
## 
## ============================================================
##               BREAK CONDITION (SALARY > 10000)               
## ============================================================
cat(sprintf("%-10s %-10s\n", "Name", "Salary"))
## Name       Salary
cat(strrep("-", 25), "\n")
## -------------------------
for (emp in employees) {
  if (emp$salary > 10000) {
    cat("\nStopped because salary above 10000\n")
    break
  }
  cat(sprintf("%-10s %-10d\n", emp$name, emp$salary))
}
## Bagas      5000      
## Joan       7000      
## Alya       6500      
## Dwi        10000     
## 
## Stopped because salary above 10000

2.6.1 Break Loop (Salary > 10,000)

Table 2.5: 2.5.3.3 - BREAK LOOP (SALARY > 10,000)
Name Salary
Bagas 5000
Joan 7000
Alya 6500
Dwi 10000
Status: Stopped because Nabil has a salary above 10,000

Budget Threshold Control (Break Logic)

  • Logic Applied: Early exit using the break statement.

  • Interpretation: The table displays employees processed before the system encountered a salary exceeding 10,000. The loop processed Bagas, Joan, Alya, and Dwi successfully. However, the sequence was halted before including Nabil, whose salary of 12,000 triggered the safety break.

  • Business Insight: This acts as a “financial circuit breaker.” It ensures that reporting or processing stops once an outlier or a specific budget limit is breached, preventing further data inclusion that might violate fiscal constraints.


2.7 5. Next — Skip Average Performance

print_header("CONTINUE (SKIP AVERAGE PERFORMANCE)")
## 
## ============================================================
##             CONTINUE (SKIP AVERAGE PERFORMANCE)              
## ============================================================
cat(sprintf("%-10s %-15s\n", "Name", "Performance"))
## Name       Performance
cat(strrep("-", 30), "\n")
## ------------------------------
for (emp in employees) {
  if (emp$performance == "Average") {
    cat(sprintf("%-10s %-15s\n", emp$name, "Skipped"))
    next
  }
  cat(sprintf("%-10s %-15s\n", emp$name, emp$performance))
}
## Bagas      Good           
## Joan       Very Good      
## Alya       Skipped        
## Dwi        Good           
## Nabil      Very Good

2.7.1 Continue (Skip Average Performance)

Table 2.6: 2.5.3.4 - CONTINUE (SKIP AVERAGE PERFORMANCE)
Name Performance
1 Bagas Good
2 Joan Very Good
4 Dwi Good
5 Nabil Very Good
Note: (Alya is skipped because the performance is ‘Average’)

Performance-Based Filtering (Skip/Next Logic)

  • Logic Applied: Iteration skipping using the next statement.

  • Interpretation: This table presents a curated list of employees, intentionally omitting those with underperforming metrics. Alya is absent from this list because her “Average” performance triggered the skip logic.

  • Business Insight: This is a “Focus-Driven” reporting method. By automatically skipping “Average” performers, management can quickly generate a “High-Value Talent List” to prioritize bonus distributions or promotion reviews without manual sorting.

3 Visualization

3.0.1 Bonus Allocation Interpretation

  • Budget Concentration: The Director and Manager positions typically consume the largest portion of the bonus budget due to the combination of higher base salaries and performance multipliers.
  • Efficiency: Even though there are more Staff members, their total bonus share might be lower if their performance falls into the “Average” category, which only triggers a 5% bonus.
  • Visual Clarity: This chart allows stakeholders to see at a glance where the company’s incentive funds are actually going.

4 Conclusion

This comprehensive analysis demonstrates the effective integration of Conditional Statements and Iterative Loops to manage and analyze an employee dataset. By utilizing R’s robust data processing capabilities, we achieved the following strategic objectives:

  • Dynamic Compensation Logic: Through the implementation of if-else structures, we successfully automated the calculation of bonuses based on performance tiers: Very Good (20%), Good (10%), Average (5%). This ensures a fair and transparent merit-based reward system.

  • Operational Control: The use of For Loops and While Loops provided a systematic way to filter data and control program execution. Specifically:

  • Filtered high-salary earners (> 6,000) for budget monitoring.

  • Executed precise scanning of organizational structures until specific roles (Manager) were identified.

  • Implemented safety breaks to prevent over-budgeting when salaries exceeded 10,000.

  • Streamlined reporting by skipping non-priority performance categories (Average) using the next/continue logic.

  • Data-Driven Visual Insights: The inclusion of Stacked Bar Charts, Point Plots, and Pie Charts transformed raw numbers into actionable business intelligence. These visuals confirm that bonus allocations are aligned with both individual performance and departmental budgeting.

In summary, this automated approach not only minimizes manual calculation errors but also provides management with a scalable tool for real-time employee performance and salary analysis.