R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Function arguments in R play a crucial role in defining and customizing the behavior of functions. R functions can have several types of arguments, including default, formal, and actual arguments. In this tutorial, we'll explore how function arguments in R work and how to use them effectively.
In R, a function is defined using the function()
construct:
my_function <- function(arg1, arg2) { # Function body return(arg1 + arg2) } # Call the function result <- my_function(5, 3) print(result) # Outputs: 8
You can specify default values for function arguments. If the caller of the function doesn't provide a value for an argument with a default value, the default value is used:
my_function <- function(a = 1, b = 2) { return(a + b) } print(my_function()) # Outputs: 3 print(my_function(4)) # Outputs: 6 (4 + 2) print(my_function(4, 5)) # Outputs: 9
Formal Arguments: These are the arguments included in the function definition. In the example my_function(a = 1, b = 2)
, a
and b
are formal arguments.
Actual Arguments: These are the values you pass in when you call the function. In the call my_function(4, 5)
, 4
and 5
are the actual arguments.
You can pass arguments to a function based on their position or their name:
# By position print(my_function(3, 4)) # Outputs: 7 # By name print(my_function(a = 3, b = 4)) # Outputs: 7 print(my_function(b = 4, a = 3)) # Outputs: 7 (order doesn't matter when using names)
...
):The ellipsis is used to denote a variable number of arguments:
concatenate_strings <- function(...) { paste(..., sep = "-") } print(concatenate_strings("a", "b", "c")) # Outputs: "a-b-c"
This is especially useful for functions that can accept a varying number of inputs, or for extending functions.
The missing()
function can be used inside a function to check if an argument was provided:
my_function <- function(x) { if (missing(x)) { return("You didn't provide an argument!") } else { return(paste("You provided:", x)) } } print(my_function()) # Outputs: "You didn't provide an argument!" print(my_function(10)) # Outputs: "You provided: 10"
In R, function arguments are lazily evaluated, which means they are only evaluated when they are used:
my_function <- function(a, b) { print("Function body starts") print(a) } my_function(print("Argument a"), print("Argument b"))
In the above example, "Argument b" will not be printed because the argument b
is never used in the function.
Understanding function arguments in R is fundamental for writing effective and flexible functions. R provides a rich set of tools to manage function arguments, catering to both simple and advanced use cases. This flexibility, combined with practices like providing default argument values and using the ellipsis, allows R functions to be both powerful and user-friendly.
Function arguments in R:
# Function with arguments in R greet <- function(name) { print(paste("Hello,", name)) } # Calling the function with an argument greet("Alice")
Passing arguments to functions in R:
# Passing arguments to functions in R add_numbers <- function(a, b) { result <- a + b print(paste("Sum:", result)) } # Calling the function with arguments add_numbers(3, 5)
Default values for function arguments in R:
# Function with default argument values greet_default <- function(name = "Guest") { print(paste("Hello,", name)) } # Calling the function without providing an argument greet_default()
Variable number of arguments in R functions:
...
) operator.# Function with a variable number of arguments sum_values <- function(...) { values <- c(...) result <- sum(values) print(paste("Sum:", result)) } # Calling the function with different numbers of arguments sum_values(1, 2, 3) sum_values(5, 10, 15, 20)
Named arguments in R functions:
# Function with named arguments divide_numbers <- function(dividend, divisor) { result <- dividend / divisor print(paste("Quotient:", result)) } # Calling the function with named arguments divide_numbers(divisor = 2, dividend = 10)
R ellipsis (...) in function arguments:
...
) operator in function arguments, enabling the acceptance of a variable number of arguments.# Function with ellipsis (...) in arguments print_values <- function(...) { values <- list(...) print(values) } # Calling the function with different numbers of arguments print_values(1, 2, 3) print_values("apple", "banana")