Programming Fundamentals in R
Understanding logical operators, control structures (like conditional statements and
loops), and creating functions are essential for programming in R. Below is a detailed guide
with examples for each of these concepts.
1. Logical Operators
Logical operators are used to perform logical operations on variables and expressions. In
R, the primary logical operators are:
• & (AND)
• | (OR)
• ! (NOT)
• == (equal to)
• != (not equal to)
• > (greater than)
• < (less than)
• >= (greater than or equal to)
• <= (less than or equal to)
Example:
R - code
x <- 5
y <- 10
# Logical AND
result_and <- (x > 2) & (y > 2) # TRUE
# Logical OR
result_or <- (x > 2) | (y < 2) # TRUE
# Logical NOT
result_not <- !(x > 2) # FALSE
# Equality
result_equal <- (x == y) # FALSE
# Print results
print(result_and) # TRUE
print(result_or) # TRUE
print(result_not) # FALSE
print(result_equal) # FALSE
2. Conditional Statements (if, else, else if)
Conditional statements control the flow of execution depending on whether a condition is
TRUE or FALSE.
Example:
• if Statement:
R - code
num <- 10
# If statement
if (num > 5) {
print("The number is greater than 5")
}
Output :code
[1] "The number is greater than 5"
• if-else Statement:
R – code
num <- 3
# If-else statement
if (num > 5) {
print("The number is greater than 5")
} else {
print("The number is 5 or less")
}
Output:code
[1] "The number is 5 or less"
• else if Statement:
R - code
num <- 7
# If-else if-else statement
if (num > 10) {
print("The number is greater than 10")
} else if (num > 5) {
print("The number is greater than 5 but less than or equal to 10")
} else {
print("The number is 5 or less")
}
Output:code
[1] "The number is greater than 5 but less than or equal to 10"
3. Loops
• While Loop: A while loop continues to execute as long as the condition remains
TRUE.
Example:
R - code
count <- 1
# While loop
while (count <= 5) {
print(count)
count <- count + 1
}
Output:code
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
• For Loop: A for loop iterates over a sequence (such as a vector, list, or range) and
executes a block of code for each element.
Example:
R - code
# For loop
for (i in 1:5) {
print(i)
}
Output:code
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
• Repeat Loop: A repeat loop keeps executing the code until a break statement is
encountered.
Example:
R - code
count <- 1
# Repeat loop
repeat {
print(count)
count <- count + 1
if (count > 5) {
break
}
}
Output:code
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
4. Creating Functions in R
Functions in R are created using the function() keyword. They are used to encapsulate
code that performs a specific task and can be reused throughout the program.
Example:
• Basic Function:
R - code
# Define a function to calculate the square of a number
square <- function(x) {
result <- x * x
return(result)
}
# Call the function
print(square(4)) # Result: 16
Output:code
[1] 16
• Function with Multiple Arguments:
R - code
# Define a function to calculate the sum of two numbers
add_numbers <- function(a, b) {
result <- a + b
return(result)
}
# Call the function
print(add_numbers(10, 20)) # Result: 30
Output:code
[1] 30
• Function with Default Arguments:
R - code
# Define a function with a default argument
greet <- function(name = "World") {
message <- paste("Hello", name)
return(message)
}
# Call the function without an argument
print(greet()) # Result: "Hello World"
# Call the function with an argument
print(greet("Alice")) # Result: "Hello Alice"
Output:code
[1] "Hello World"
[1] "Hello Alice"
• Function with Return Value:
R - code
# Define a function to calculate the factorial of a number
factorial <- function(n) {
if (n == 0) {
return(1)
} else {
return(n * factorial(n - 1))
}
}
# Call the function
print(factorial(5)) # Result: 120
Output:code
[1] 120
Summary
Understanding the basics of logical operators, conditional statements, loops, and function
creation in R is crucial for writing effective and efficient code. Logical operators allow you
to make comparisons, while conditional statements (if, else, else if) control the flow
of your program. Loops (while, for, repeat) help automate repetitive tasks, and
functions enable code reuse and modularity. These are the building blocks for more
advanced programming in R.