Programing Sains Data 1
Major
Data Science
Student ID
52250031
Lecturer
Bakti Siregar, M. Sc., CSD.
Subject
Basic Statistics
1 Calculation of Bonuses for Each Employee
π°
PRACTICUM 2.5
Employee Bonus Calculation
with R
with R
π
Employee Dataset
π Bonus Rules
Very Good
20%
Good
10%
Average
5%
π
R Program Code
π
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
with R
π
Employee Dataset
π Task Requirements
FOR Loop
Display employees with salary > 6000
WHILE Loop
Display employees until βManagerβ is found
π
R Program Code β FOR Loop
π
FOR Loop Results
π€
Joan
$7,000
Supervisor
π€
Alya
$6,500
Staff
π€
Dwi
$10,000
Manager
π€
Nabil
$12,000
Director
π
R Program Code β WHILE Loop
π
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
with R
π
Employee Dataset
π Task Requirements
- BREAK
Stop the loop when salary > 10,000 is found
- NEXT (continue)
Skip employees with βAverageβ performance
π
R Program Code β BREAK
π
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)
π
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 β