CONTROL
STRUCTURES
CONTROL STRUCTURES
• Control structures are used for
• Making decisions (conditional statements such as if, if-else)
• Repeat tasks (loops such as ‘for loop’)
CONDITIONAL STATEMENTS
ifelse() checks each row in a dataframe and applies logic across the whole column.
Examples: In the mtcars,
1. Create a new column “mpg_level”, which indicates ‘high’ if mpg > 20 and ‘low’ if mpg<=20
2. Create a new column “grade”, based on the following conditions:
• Assign "A" if mpg is greater than 25
• Assign "B" if mpg is between 21 and 25
• Assign "C" if mpg is between 16 and 20
• Assign "D" if mpg is 15 or less
CONDITIONAL STATEMENTS
if() works only with a single logical value.
Examples: In the mtcars dataset,
1. Check if the first car has a mileage greater than 25.
Print "High mileage" if true, otherwise print "Low
mileage".
2. Check if the number of rows is greater than 30. If yes,
print "Large dataset"; otherwise, print "Small dataset".
3. Compare the hp (horsepower) of the 5th and 10th
cars in the data frame df. Print which car has more
horsepower.
“FOR” LOOP
• A for loop is a control structure that repeats a block of code, a fixed number of times.
Examples:
1. Print your name 3 times
1 2
2. Print numbers 1 to 5 3
3. Print the square of each number in a vector
4. Print the name of the car and its corresponding mpg for the first 3 rows of in mtcars
dataset. The output should follow this format: "Mazda RX4 has mpg: 21"
4 paste function concatenates two or more strings/variables
cat function concatenates as well as prints
\n is called new line character which moves output to the next line
COMBINATION OF “IF” AND “FOR”
Examples:
1
1. For each number, in a sequence of 1 to 10, print a
message stating whether the number is odd or even.
2. In the vector (5, 10, 13, 24, 33, 47), count how many
numbers are odd and how many are even. Print the
final counts.
3. Compute the squares of elements in the vector 2
(2,4,6,8). Print only those squares that are > 30
3
COMBINATION OF “IF” AND “FOR”
1
Reference dataset: mtcars
1. Print names of cars with mpg > 25. The
output should follow this format: "Fiat 128
has high mileage"
2
2. Count how many cars have more than 6
cylinders.
3. Print cars that have mpg > 25 and weight
< 2.5
3
EXERCISES (REFERENCE DATASET: EMPLOYEES)
Create a copy of Employees and name it as Emp. Create the following new columns in Emp:
1. ‘SatCategory’ based on SatLevel. Assign "High" if SatLevel > 1.5 else assign "Low“.
2. ‘ExperienceLevel’ with categories "Experienced" if Tenure >= 5, "New" otherwise.
3. BonusEligible: "Yes" if Promotion == "1" and NumProjects >= 5, "No" otherwise.
4. CriticalDept: "Yes" if Department is "sales" or "technical“, "No" otherwise.
Answer the following questions based on above columns:
a. How many employees are eligible for bonus. How many are not eligible?
b. What is the mean satisfaction level of employees in critical and non critical departments.
c. What is the mean tenure of employees of experienced and new employees.
EXERCISES (…CONTD)
5. Check if the first employee has worked for 5 or more years (Tenure >= 5). Print
"Experienced" if true, otherwise print "New".
6. Check if the 10th employee has a SatLevel greater than 1.5. If yes, print "Highly
satisfied"; else print "Low satisfaction".
7. Check whether the 2nd employee belongs to "hr" department and has tenure > 4
years. Print "HR with experience" or "Not matching" accordingly.
EXERCISES (…CONTD)
8. For the first 10 employees, print "Experienced" if Tenure is 4 or more, otherwise print
"New”. Required format of the output:
9. Count how many employees have NumProjects > 5. Print the total count.
10. Print the Department and SalaryLevel of the first 3 employees in this format: