Programing Sains Data 1

M.Fitrah Aidil Harahap

M. FITRAH AIDIL HARAHAP

Data Science Major

Data Science
Major Data Science
Data Science
Student ID 52250031
Data Science
Lecturer Bakti Siregar, M. Sc., CSD.
Subject Basic Statistics

1 Calculation of Bonuses for Each Employee

πŸ’°

PRACTICUM 2.5
Employee Bonus Calculation
with R

πŸ“Š

Employee Dataset
ID Name Age Salary Position Performance
1 Bagas 25 $5,000 Staff Good
2 Joan 30 $7,000 Supervisor Very Good
3 Alya 27 $6,500 Staff Average
4 Dwi 35 $10,000 Manager Good
5 Nabil 40 $12,000 Director Very Good

πŸ“‹ Bonus Rules

Very Good
20%
Good
10%
Average
5%

πŸ“Ÿ

R Program Code

πŸ“Œ R Script - Bonus Calculation RUN

# Create employee data frame
employees <- 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"),
  stringsAsFactors = FALSE
)

# Calculate bonus based on performance
for (i in 1:nrow(employees)) {
  performance <- employees$Performance[i]
  salary <- employees$Salary[i]
  bonus <- 0
  
  if (performance == "Very Good") {
    bonus <- 0.2 * salary
  } else if (performance == "Good") {
    bonus <- 0.1 * salary
  } else if (performance == "Average") {
    bonus <- 0.05 * salary
  }
  
  # Display results
  cat(paste0("Name: ", employees$Name[i], ", Bonus: $", bonus, "\n"))
}

πŸ“ˆ

Bonus Calculation Results
πŸ‘€
Bagas
$500
Good (10%)
πŸ‘€
Joan
$1,400
Very Good (20%)
πŸ‘€
Alya
$325
Average (5%)
πŸ‘€
Dwi
$1,000
Good (10%)
πŸ‘€
Nabil
$2,400
Very Good (20%)

#RProgramming #EmployeeBonus #DataScience #Practicum25

← View Results β†’

2 Loops For dan While

πŸ”

PRACTICUM 2.5.3
Loops (For & While)
with R

πŸ“Š

Employee Dataset
ID Name Age Salary Position Performance
1 Bagas 25 $5,000 Staff Good
2 Joan 30 $7,000 Supervisor Very Good
3 Alya 27 $6,500 Staff Average
4 Dwi 35 $10,000 Manager Good
5 Nabil 40 $12,000 Director Very Good

πŸ“‹ Task Requirements

FOR Loop
Display employees with salary > 6000
WHILE Loop
Display employees until β€œManager” is found

πŸ“Ÿ

R Program Code β€” FOR Loop

πŸ“Œ R Script β€” FOR Loop (Salary > 6000) RUN

# Create employee data frame
employees <- 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"),
  stringsAsFactors = FALSE
)

# FOR loop β€” display employees with salary > 6000
for (i in 1:nrow(employees)) {
  if (employees$Salary[i] > 6000) {
    cat(paste0("Name: ", employees$Name[i],
            ", Salary: ", employees$Salary[i], "\n"))
  }
}

πŸ“ˆ

FOR Loop Results
πŸ‘€
Joan
$7,000
Supervisor
πŸ‘€
Alya
$6,500
Staff
πŸ‘€
Dwi
$10,000
Manager
πŸ‘€
Nabil
$12,000
Director

πŸ“Ÿ

R Program Code β€” WHILE Loop

πŸ“Œ R Script β€” WHILE Loop (Until Manager Found) RUN

# WHILE loop β€” display employees until "Manager" is found
i <- 1
while (i <= nrow(employees)) {
  if (employees$Position[i] == "Manager") {
    cat(paste0("Name: ", employees$Name[i],
            ", Position: ", employees$Position[i],
            " (Stop here)\n"))
    break
  } else {
    cat(paste0("Name: ", employees$Name[i],
            ", Position: ", employees$Position[i], "\n"))
  }
  i <- i + 1
}

πŸ“ˆ

WHILE Loop Results
πŸ‘€
Bagas
Staff
βœ“ Continue
πŸ‘€
Joan
Supervisor
βœ“ Continue
πŸ‘€
Alya
Staff
βœ“ Continue
πŸ›‘
Dwi
Manager
Stop here!

πŸ’‘

Code Explanation
πŸ”΅ FOR Loop β€” Display Employees with Salary > 6000
STEP 1 β€” DATA SETUP
employees <- data.frame(…)
A data frame is created with 5 employees, storing ID, Name, Age, Salary, Position, and Performance. stringsAsFactors = FALSE keeps text columns as plain strings.
STEP 2 β€” LOOP ITERATION
for (i in 1:nrow(employees))
Iterates from row 1 to the total number of rows using nrow(). Every employee is checked on every iteration β€” no rows are skipped.
STEP 3 β€” CONDITION CHECK
if (employees$Salary[i] > 6000)
Filters only rows where Salary exceeds 6000. Bagas ($5,000) is skipped; Joan, Alya, Dwi, and Nabil pass the condition and are printed.
STEP 4 β€” OUTPUT
cat(paste0(β€œName:”, …, β€œ, Salary:”, …))
paste0() combines text and values without spaces. cat() prints the result to the console with a newline .
🟑 WHILE Loop β€” Display Employees Until β€œManager” Found
STEP 1 β€” INDEX INIT
i <- 1
Unlike the FOR loop, the WHILE loop uses a manual index starting at 1 (R is 1-indexed). This index is used to access each row of the data frame.
STEP 2 β€” LOOP CONDITION
while (i <= nrow(employees))
The loop runs as long as i does not exceed the total rows. This prevents an out-of-bounds error if β€œManager” is never found.
STEP 3 β€” STOP CONDITION
if (employees$Position[i] == β€œManager”) { break }
When Position equals β€œManager” (row 4 = Dwi), the employee is printed with β€œ(Stop here)” and break exits the loop immediately.
STEP 4 β€” INCREMENT
i <- i + 1
The index is manually incremented at the end of each iteration. Without this, the loop would run forever (infinite loop) on the same row.

#RProgramming #ForLoop #WhileLoop #Practicum253

← View Results β†’

2.1 Loops break and continue

⚑

PRACTICUM 2.5.4
Break & Continue (Next)
with R

πŸ“Š

Employee Dataset
ID Name Age Salary Position Performance
1 Bagas 25 $5,000 Staff Good
2 Joan 30 $7,000 Supervisor Very Good
3 Alya 27 $6,500 Staff Average
4 Dwi 35 $10,000 Manager Good
5 Nabil 40 $12,000 Director Very Good

πŸ“‹ Task Requirements

  1. BREAK
Stop the loop when salary > 10,000 is found
  1. NEXT (continue)
Skip employees with β€œAverage” performance

πŸ“Ÿ

R Program Code β€” BREAK

πŸ“Œ R Script β€” Stop loop when Salary > 10,000 RUN

# Create employee data frame
employees <- 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"),
  stringsAsFactors = FALSE
)

# Use break to stop when salary above 10,000 is found
for (i in 1:nrow(employees)) {
  if (employees$Salary[i] > 10000) {
    cat(paste0("(Stopped because ", employees$Name[i],
            " has a salary above 10,000)\n"))
    break
  }
  cat(paste0("Name: ", employees$Name[i],
          ", Salary: ", employees$Salary[i], "\n"))
}

πŸ“ˆ

BREAK Results
πŸ‘€
Bagas
$5,000
βœ“ Printed
πŸ‘€
Joan
$7,000
βœ“ Printed
πŸ‘€
Alya
$6,500
βœ“ Printed
πŸ‘€
Dwi
$10,000
βœ“ Printed
πŸ›‘
Nabil
$12,000
break!
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)

πŸ’‘

Code Explanation β€” BREAK
HOW BREAK WORKS
if (Salary > 10000) { break }
The loop checks each employee’s salary. When Nabil’s salary ($12,000) exceeds 10,000, break immediately exits the loop β€” no further rows are processed.
PRINT ORDER
cat(…) is called BEFORE the if-break check
The if block is checked first. If salary > 10,000 β†’ print stop message and break. Otherwise β†’ print the employee’s name and salary normally.

πŸ“Ÿ

R Program Code β€” NEXT (continue)

πŸ“Œ R Script β€” Skip employees with β€œAverage” performance RUN

# Use next to skip employees with "Average" performance
for (i in 1:nrow(employees)) {
  if (employees$Performance[i] == "Average") {
    next
  }
  cat(paste0("Name: ", employees$Name[i],
          ", Performance: ", employees$Performance[i], "\n"))
}
cat('(Alya is skipped because the performance is "Average")\n')

πŸ“ˆ

NEXT Results
πŸ‘€
Bagas
Good
βœ“ Printed
πŸ‘€
Joan
Very Good
βœ“ Printed
⏭️
Alya
Average
next! skipped
πŸ‘€
Dwi
Good
βœ“ Printed
πŸ‘€
Nabil
Very Good
βœ“ Printed
Name: Bagas, Performance: Good
Name: Joan, Performance: Very Good
Name: Dwi, Performance: Good
Name: Nabil, Performance: Very Good
(Alya is skipped because the performance is β€œAverage”)

πŸ’‘

Code Explanation β€” NEXT
HOW NEXT WORKS
if (Performance == β€œAverage”) { next }
next skips the rest of the current iteration and jumps to the next row. Unlike break, the loop continues β€” only Alya is skipped.
BREAK vs NEXT
break β†’ exits the entire loop next β†’ skips current row only
Alya (row 3, β€œAverage”) triggers next, so her row is not printed. Rows 4 (Dwi) and 5 (Nabil) are still processed normally.

#RProgramming #BreakNext #LoopControl #Practicum254

← View Results β†’