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
In R, functions are objects that can take in parameters and return a result. Functions are crucial in R as they help in organizing and abstracting code. R offers a variety of function types catering to various needs. This tutorial will give you an overview of the different types of functions in R.
These are the standard functions that come with the R base package. You don't need to install or load any additional libraries to use them.
Examples:
+, -, *, /
mean(), sd(), var()
seq(), rep()
As the name suggests, these are the functions defined by the users to perform a specific task.
# Simple function to add two numbers add_numbers <- function(a, b) { return(a + b) } # Use the function result <- add_numbers(5, 3) print(result) # Outputs: 8
Also known as lambda functions in some languages, these are functions without a name. They're typically used for short operations that you don't want to define a full function for.
# Using an anonymous function with the `sapply` function result <- sapply(1:5, function(x) x^2) print(result) # Outputs: 1 4 9 16 25
A recursive function is a function that calls itself.
# Function to compute factorial factorial_recursive <- function(n) { if (n == 0) { return(1) } else { return(n * factorial_recursive(n-1)) } } print(factorial_recursive(5)) # Outputs: 120
Functions defined inside another function are called nested functions.
outer_function <- function(a, b) { inner_function <- function(x) { return(x^2) } return(inner_function(a) + inner_function(b)) } print(outer_function(3, 4)) # Outputs: 9 + 16 = 25
These are basic, low-level functions written in C that are called from R. For example, functions like sum()
are primitive functions. You typically won't write these yourself unless you're developing R or creating a low-level package.
Functions in R can capture environment data, making them closure functions. Any function that uses data not passed explicitly as an argument can be considered a closure.
make_multiplier <- function(multiplier) { function(x) { return(x * multiplier) } } double_it <- make_multiplier(2) print(double_it(5)) # Outputs: 10
Functions are fundamental to R programming. The versatility of function types in R allows for powerful and flexible programming, enabling you to build everything from simple scripts to complex data processing pipelines. Familiarizing yourself with the different types of functions and understanding when to use each is key to becoming proficient in R.
User-Defined Functions in R:
# Example: User-defined function my_function <- function(x, y) { result <- x + y return(result) }
Statistical Functions in R:
# Example: Statistical functions mean_value <- mean(data_vector)
Mathematical Functions in R:
# Example: Mathematical functions square_root <- sqrt(25)
String Functions in R:
# Example: String functions string_length <- nchar("Hello, World!")
Data Manipulation Functions in R:
subset
and filter
manipulate data.# Example: Data manipulation functions filtered_data <- subset(data_frame, condition)
Plotting Functions in R:
plot
for data visualization.# Example: Plotting functions plot(x, y, type = "scatter")
Time Series Functions in R:
# Example: Time series functions ts_object <- ts(data, frequency = 12)
List Manipulation Functions in R:
lapply
and unlist
operate on lists.# Example: List manipulation functions modified_list <- lapply(my_list, function(x) x * 2)
Function Types in R Base Package:
# Example: Function types sample_mean <- mean(sample_data)
Functional Programming in R:
# Example: Functional programming squared_values <- map(my_vector, ~ .x^2)
Applying Functions to Data Frames in R:
# Example: Applying functions to data frames row_sums <- apply(data_frame, 1, sum)
Vectorized Functions in R:
# Example: Vectorized functions multiplied_vector <- my_vector * 2
R purrr Package for Advanced Function Operations:
purrr
package offers advanced functions for iteration and manipulation.# Example: Using purrr for advanced functions library(purrr) modified_list <- map(my_list, ~ .x * 2)