UNIT-IV
Conditionals and Control Flow: Relational Operators, Relational Operators and
Vectors, Logical Operators, Logical Operators and Vectors, Conditional Statements.
Iterative Programming in R: Introduction, While Loop, For Loop, Looping Over List
Functions in R: Introduction, writing a Function in R, Nested Functions, Function
Scoping, Recursion, Loading an R Package, Mathematical Functions in R.
Relational Operators
Relational operators in R are used to compare values. They return a logical value
(TRUE or FALSE) depending on whether the specified relationship between the
operands holds true. The most common relational operators in R are:
Example:
Relational Operators and Vectors
Relational operators can also be applied element-wise to vectors. This means that
each element of a vector is compared to the corresponding element in another vector.
Example:
In this case, the first element of vec1 is compared to the first element of vec2, and so
on.
Logical Operators
Logical operators are used to perform logical operations, often combining multiple
relational expressions. They return logical values (TRUE or FALSE).
Example:
Logical Operators and Vectors
Logical operators can be applied to vectors element-wise. This is useful when you
want to check multiple conditions for multiple elements.
Example:
Here, the first elements of vec1 and vec2 are compared, then the second, and so on.
Conditional Statements:
Conditional statements allow you to control the flow of your program based on
certain conditions. The most common conditional statements in R are if, else if, and
else.
if: Checks a condition and executes the code if the condition is TRUE.
else if: Checks another condition if the previous if condition was FALSE.
else: Executes code if none of the previous conditions were TRUE.
Functions in R
Functions in R are key building blocks that help you reuse code and
perform specific tasks. Functions can take arguments, execute code, and
return results, enabling modular and organized programming.
Introduction to Functions in R
A function in R is a block of code that performs a particular task. R has
numerous built-in functions, but you can also create your own custom
functions using the function() keyword.
Example:
add_numbers <- function(x, y) {
result <- x + y
return(result)
}
Writing a Function in R
You can define a function in R using the syntax function_name <-
function(arguments) { body }. Functions can accept multiple arguments
and return values.
Example:
greet <- function(name) {
message <- paste("Hello,", name, "!") return(message)
}
greet("Alice")
Output:
[1] "Hello, Alice!"
Nested Functions
In R, you can define functions inside other functions. These are called
nested functions. They allow for modular coding and help break down
complex tasks.
Example:
outer_function <- function(x) {
inner_function <- function(y) {
return(y * 2)
}
result <- inner_function(x) return(result)
}
outer_function(5)
Output:
[1] 10
Function Scoping
R uses lexical scoping, which means that the value of a variable is looked
up in the environment where the function was defined, not where it is
called.
Example
x <- 10
scope_example <- function() {
x <- 5
return(x)}
scope_example() # Returns 5 because x is 5 inside the function scope
x # Returns 10 as the global x remains unchanged
Output:
[1] 5
[1] 10
5. Recursion
A recursive function is a function that calls itself. Recursion is useful for
tasks that can be divided into similar subtasks, such as calculating
factorials.
Example: Factorial Function
factorial <- function(n) {
if (n <= 1) {
return(1)
} else {
return(n * factorial(n - 1))
}}
factorial(5)
Output:
[1] 120
Loading an R Package
R packages contain functions, data, and code that you can use in your R
scripts. You can load a package using library() or require().
Example:
# Install a package if not already installed
if (!require(ggplot2)) install.packages("ggplot2")
# Load the package
library(ggplot2)
Mathematical Functions in R
R has several built-in mathematical functions to perform common
mathematical tasks. Some common functions include sum(), mean(),
sqrt(), and log().
# Sum of a vector
sum(c(1, 2, 3, 4, 5)) # Output: 15
# Mean of a vector
mean(c(1, 2, 3, 4, 5)) # Output: 3
# Square root
sqrt(16) # Output: 4
# Natural logarithm
log(10) # Output: ~2.3026