User-Defined Functions in R
In R, user-defined functions are functions that you create to perform specific tasks that are not already covered by R's built-in functions. These functions allow you to reuse code and create more complex operations that are tailored to your needs.
A user-defined function is created using the function() keyword, and it can have input parameters, a body (the code to be executed), and a return value. Here's the basic structure of a user-defined function in R:
R
function_name <- function(arg1, arg2, ...) {
# Body of the function
# Operations to be performed using arguments
result <- some_computation
return(result) # Return the result
}
Here's a step-by-step explanation of the syntax:
Function Name: Choose a name for the function that will be used to call it.
Function Arguments: The values or variables that the function needs to perform its task.
Function Body: The code block inside the function that defines what the function will do.
Return Statement: The result the function produces after completing its task.
Example 1: Simple Function Without Arguments
R
# Define a function to return "Hello, World!"
greeting <- function() {
return("Hello, World!")
}
# Call the function
greeting() # Output: "Hello, World!"
In this example, the function greeting() doesn't take any arguments and simply returns a string.
You can define functions with arguments (input parameters) that allow you to pass values when calling the function. This allows your function to perform tasks using dynamic data.
Example 2: Function with Arguments
R
# Define a function that takes two arguments and returns their sum
add_numbers <- function(a, b) {
sum <- a + b
return(sum)
}
# Call the function with arguments 3 and 5
result <- add_numbers(3, 5)
print(result) # Output: 8
In this case, the function add_numbers() takes two parameters (a and b) and returns their sum.
You can set default values for function arguments. If the user does not provide a value for the argument, the default value will be used.
Example 3: Function with Default Argument
R
# Define a function with a default value for the second argument
multiply_numbers <- function(a, b = 2) {
product <- a * b
return(product)
}
# Call the function with both arguments
result1 <- multiply_numbers(5, 3)
print(result1) # Output: 15
# Call the function with only the first argument (second argument defaults to 2)
result2 <- multiply_numbers(5)
print(result2) # Output: 10
Here, multiply_numbers() has a default value of 2 for the second argument b. If b is not specified when calling the function, it uses the default value.
R functions can return only one object (or value) by default, but you can return multiple values by storing them in a list, vector, or data frame.
Example 4: Function Returning Multiple Values
R
# Define a function that returns both the sum and the product of two numbers
sum_and_product <- function(a, b) {
sum_result <- a + b
product_result <- a * b
return(list(sum = sum_result, product = product_result))
}
# Call the function and store the result
result <- sum_and_product(4, 5)
# Access the returned values
print(result$sum) # Output: 9
print(result$product) # Output: 20
Here, the function sum_and_product() returns a list containing both the sum and the product of a and b.
Anonymous functions are functions that are defined without a name and are usually used when you need a short function for a specific task. These are often used as arguments to other functions (e.g., apply(), lapply()).
Example 5: Anonymous Function
R
# Use an anonymous function with the `sapply()` function
result <- sapply(c(1, 2, 3, 4), function(x) x^2)
# Output: 1 4 9 16
print(result)
In this example, an anonymous function is used to square each element of the vector c(1, 2, 3, 4).
Variables defined inside a function are local to that function and cannot be accessed outside the function. These are called local variables. However, you can use variables that are defined outside the function, which are called global variables.
Example 6: Local and Global Variables
R
# Global variable
x <- 10
# Define a function that uses a local variable
local_function <- function() {
y <- 5 # Local variable
return(x + y) # Accessing the global variable 'x' and local variable 'y'
}
result <- local_function()
print(result) # Output: 15 (10 from global x + 5 from local y)
Here, the function local_function() uses the global variable x and its own local variable y.
You can create functions to perform operations iteratively, for example, applying a function to each element of a vector or list.
Example 7: Iterating Over a Vector
R
# Define a function to apply to each element of a vector
apply_square <- function(v) {
result <- sapply(v, function(x) x^2)
return(result)
}
# Apply the function to a vector
numbers <- c(1, 2, 3, 4)
squared_numbers <- apply_square(numbers)
print(squared_numbers) # Output: 1 4 9 16
In this example, the function apply_square() applies the square operation to each element of the vector numbers using sapply().
A recursive function is a function that calls itself in order to solve a problem. Recursive functions are typically used for problems that can be broken down into smaller, similar sub-problems (e.g., factorial, Fibonacci sequence).
Example 8: Recursive Function (Factorial)
R
# Define a recursive function to calculate factorial
factorial <- function(n) {
if (n == 1) {
return(1) # Base case
} else {
return(n * factorial(n - 1)) # Recursive call
}
}
# Calculate the factorial of 5
result <- factorial(5)
print(result) # Output: 120 (5 * 4 * 3 * 2 * 1)
In this example, the factorial() function calls itself until it reaches the base case where n == 1.
Defining Functions: Use the function() keyword to define a function.
Arguments: Functions can accept multiple input parameters and even have default values.
Returning Values: Use return() to return values. You can return lists, vectors, or other objects for multiple outputs.
Anonymous Functions: These are functions without a name, often used for short tasks.
Function Scope: Variables inside a function are local, and you can use global variables inside functions.
Recursion: Functions can call themselves for recursive tasks like factorial or Fibonacci numbers.