
Learning Goals
- Learn
if-elsestatement syntax - Learn
forloop syntax - Learn
whileloop syntax
Control Structures
Control structures in programming are tools that manage the flow of execution in your code based on specific conditions or repetitions. They allow your program to make decisions using if-else statements, or to perform tasks repeatedly with loops like for and while. By using control structures, you can make your code dynamic, efficient, and able to respond intelligently to different inputs or scenarios.
If-Else Statements
If-else statements are common conditional statements that allow codes to choose between two or more actions based on a specified condition.
In R, the syntax for an if-else statement follows this structure:
Here’s a simple example of an if-else statement to check whether a number is odd or even. Try changing the number and re-running the code.
Exercise 3.1
Write a code using if-else statements that check a person’s age and prints out their age category. The categories are:
- “Non-adult” if the person is under 21 years old
- “Adult” if the person is 21 and above
- “Older-adult” if the person is 65 and above
if (age >= 65) {
print("This person is an older adult")
} else if (age >= 21) {
print("This person is an adult")
} else {
print("This person is a non-adult")
}For Loops
For loops in programming are control structures that allow you to execute a block of code repeatedly for a specified number of iterations. They are particularly useful for iterating over sequences, such as lists or vectors, enabling you to perform repetitive tasks efficiently. By defining a loop variable and a range, for loops help automate processes that require the same operation to be applied multiple times, streamlining code and enhancing productivity.
In R, the syntax for a for loop follows this structure:
Here’s an example of printing numbers from 1 to 10:
Numeric Data - Take note!
By default, any number you enter in R is considered numeric. You may add the L suffix after a whole number to denote it as an integer instead.
Checking data types
One common way of checking the data type of an object is using the class() function.
Exercise 2.1
The code below creates objects of various data types.
Use the class() function to check the data type of the objects above and assign your answers to the respective objects.
class_a <- class(a)
class_b <- class(b)
class_c <- class(c)Checking specific data types
There are several alternative ways of checking data types. One useful way of verifying specific data types is employing the is.*() functions. These functions return a logical value.
Converting data types
As mentioned above, certain functions only support specific data types. Thus, it is important to ensure the compatibility between your data type and the functions to execute. You can change the data type using the as.*() functions.
Run the code below to convert the object to the respective data types.
Both object outputs look similar but differ in data types. The code below performs a multiplication operation on the objects. Run the code to examine the difference between them.
Exercise 2.2
You are given two objects named obj_1 and obj_2 respectively. Run the code below to see their output.
Check the data types of the two objects.
Use either class() or is.*() functions. Refer to the sections above for examples on these functions.
class(obj_1)
class(obj_2)
# Alternatively, you may check whether they are numeric objects
is.numeric(obj_1)
is.numeric(obj_2)Ensure that both objects are numeric data types and assign them to the respective objects. Convert the data type of the objects if needed, and perform a sum operation on both objects.
Use as.*() functions. Refer to the sections above for examples on these functions.
num_obj_1 <- as.numeric(obj_1)
num_obj_2 <- as.numeric(obj_2)
sum(num_obj_1, num_obj_2)Bonus: Factor Data
Factor data is a specific data type in R that represents categorical data. It contains a fixed set of levels, stored as integer codes with corresponding labels.
By default, factor levels follow either numerical values or alphabetical orders. You may specify your own levels of the factor.