0% found this document useful (0 votes)
3 views

Functions in r

The document discusses various programming concepts in R, including nested functions, lexical scoping, recursion, and packages. It explains how nested functions allow for better code structure, the rules of lexical scoping, and the importance of base and recursive cases in recursion. Additionally, it covers the installation and usage of packages in R, along with a brief overview of mathematical functions available in the language.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Functions in r

The document discusses various programming concepts in R, including nested functions, lexical scoping, recursion, and packages. It explains how nested functions allow for better code structure, the rules of lexical scoping, and the importance of base and recursive cases in recursion. Additionally, it covers the installation and usage of packages in R, along with a brief overview of mathematical functions available in the language.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

FUNCTIONS IN R

NESTED FUNCTIONS IN R:

• Nested functions in R are functions that are defined within other functions. This feature is useful for structuring code
more efficiently, allowing for encapsulation and reuse of logic within the context of another function.
Example:

outer_function <- function(x) {


# Define the inner function
inner_function <- function(x) {
return(x^2)
}
# Use the inner function inside the outer function
result <- inner_function(x) + x
return(result)
}
# Example usage # OUTPUT: [1] 30
outer_function(5)
SCOPING OF FUNCTION:

In R, functions follow lexical scoping rules. This means that when you refer to a variable inside a
function, R looks for the value of that variable in the environment where the function was created, not
where it was called.

How Lexical Scoping Works:


• Local Environment: R first looks for the variable in the local environment (inside the current function).
• Enclosing Environment: If not found, it searches in the environment where the function was defined.
• Parent Environments: If still not found, R checks the parent environments, moving up the chain.
• Global Environment: If the variable is not found in any enclosing environments, R finally looks in the
global environment.
• Base Environment: If not found in the global environment, R looks in the base environment (built-in R
functions).
• Error: If the variable is not found anywhere, R throws an error.
EXAMPLE PROGRAM:

# Global variable
x <- 10
# Function definition
outer_function <- function() {
x <- 20 # Local variable in outer_function
inner_function <- function() {
return(x)
}
return(inner_function()) # OUTPUT : [1] 20
}
# Calling the function
outer_function()
# Global variable
x <- 10

# Function definition
outer_function <- function() {

inner_function <- function() {


return(x)
}

return(inner_function()) # OUTPUT : [1] 10


}

# Calling the function


outer_function()
RECURSION IN R:

• Recursion is a technique in programming where a function calls itself in order to solve a


problem. In R, recursion is used to break down complex problems into simpler
subproblems, making it useful for tasks like calculating factorials, Fibonacci sequences,
and tree traversals.
How Recursion Works:
• In recursion:
• Base Case: This is the condition that stops the recursion. Without a base case, the
function would call itself indefinitely.
• Recursive Case: This is the part of the function where it calls itself with a modified
argument.
RECURSION IN R:
sum_recursive <- function(n) {
# Base case: when n is 1
if (n == 1) {
return(1)
}
else {
return(n + sum_recursive(n - 1))
}
} # OUTPUT: [1] 15
# Example usage
sum_recursive(5)
PACKAGES IN R:
Packages in R are collections of R functions, data, and compiled code in a well-defined format. They extend
the functionality of R by providing additional functions and datasets for various tasks such as data
manipulation, visualization, statistical analysis, machine learning, and more.
The most common way to install a package is by using the install.packages() function.
There are basically two extremely important functions when it comes down to R packages:

• install.packages(), which as you can expect, installs a given package.
• library() which loads packages, i.e. attaches them to the search list on your R workspace.

# Basic installation from CRAN


install.packages("ggplot2")

# Load the ggplot2 package


library(ggplot2)
Mathematical Functions in R:

• R provides the various mathematical functions to perform the mathematical calculation. These
mathematical functions are very helpful to find absolute value, square value and much more
calculations. In R, there are the following functions which are used:
• Basic Arithmetic Functions
• Trigonometric Functions
• Logarithmic and Exponential Functions
• Rounding Functions
• Statistical Functions
• Special Mathematical Functions
• Using Mathematical Constants
THANK YOU

You might also like