7 Programming Fundamentals in R
7 Programming Fundamentals in R
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
Example:
• if Statement:
R - code
num <- 10
# If statement
if (num > 5) {
print("The number is greater than 5")
}
Output :code
• 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
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)
}
Output:code
[1] 16
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
R - code
# Define a function with a default argument
greet <- function(name = "World") {
message <- paste("Hello", name)
return(message)
}
Output:code
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.