R Functions

Learn via video courses
Topics Covered

Overview

R functions are blocks of reusable code that execute specific tasks, enhancing efficiency and organization in R programming. Users can either employ built-in functions or craft their own, tailored to their specific needs. Understanding and using functions is crucial for tapping into the full potential of R, whether for data manipulation, statistical analysis, or visualization.

What is a Function in R?

A function in R is essentially a named block of code, designed to perform a defined operation. By taking in input parameters or arguments, processing data, and returning a result, functions contribute to the modularity, reusability, and organization of code. Both built-in functions (from R packages and libraries) and user-defined functions empower users to streamline diverse tasks in R.

Creating a Function in R

In R, a function is a self-contained block of code that performs a specific task or computation. It allows you to encapsulate a series of commands, making your code modular, reusable, and easier to maintain. To create a function in R, you use the function() keyword followed by a set of parentheses where you can define input parameters (arguments) that the function will accept. Inside the function body, you write the code that defines the task to be performed using these arguments. You can use the return() statement to specify the output or result that the function will return after processing the input data. Functions can be as simple as a few lines of code or more complex, involving conditional statements, loops, and other functions.

Once you define a function, you can call it by its name and pass values as arguments to execute the defined computation. Functions are essential in R programming as they promote code reusability, improve readability, and allow you to efficiently organize your code for better maintainability.

Let us see an example syntax below:

In the above code this function, add_numbers, adds two numbers together.

Function Components

In R, a function is a self-contained block of code that performs a specific task or set of operations. Functions play a crucial role in R programming, enabling users to modularize their code, improve readability, and promote code reuse.

A typical function in R consists of several components:

  • Function Name:
    A unique identifier used to call the function in the code. It follows R's naming rules and conventions.

  • Arguments:
    Input parameters that the function takes to perform its task. These can be data, values, or other functions. Functions can have zero or more arguments.

  • Function Body:
    The set of instructions written within curly braces {}. It contains the actual code that defines the function's behavior.

  • Return Value:
    The output of the function, generated by executing the code in the function body. It can be a single value or a complex data structure.

  • Function Call:
    The process of invoking a function by using its name followed by parentheses (). Arguments can be passed to the function during the call.

  • Documentation:
    Well-documented functions include comments or metadata that describe the purpose of the function, its arguments, and the expected return value.

  • To define a custom function in R, use the function keyword, followed by the argument list and the function body. The return keyword is optional; if not used, the function will return the result of the last expression in the function body.

For example:

Using functions in R enhances code organization, and readability, and promotes efficient development by encapsulating specific functionality into reusable units.

Types of Function in R Language

Built-in Function in R

In R, built-in functions are pre-defined functions that are included in the base R system or various packages and libraries. These functions provide a wide range of functionalities to users, covering tasks such as data manipulation, statistical analysis, plotting, and more. Some common types of built-in functions in R are:

  • Mathematical Functions:
    R includes a variety of mathematical functions for performing basic arithmetic operations (e.g., addition, subtraction, multiplication, division) as well as more complex calculations (e.g., exponentiation, logarithms, trigonometric functions). Examples include sqrt(), log(), sin(), cos(), and sum().

  • Statistical Functions:
    R is widely used for statistical analysis, and it provides numerous built-in functions for calculating summary statistics (e.g., mean, median, variance, standard deviation) and performing hypothesis tests (e.g., t-tests, chi-squared tests). Examples include mean(), median(), var(), cor(), and t.test().

  • Data Manipulation Functions:
    R offers various functions for data manipulation, such as filtering, sorting, merging, and transforming datasets. Examples include subset(), order(), merge(), and transform().

  • Character Manipulation Functions:
    These functions allow manipulation of character strings, such as extracting substrings, converting cases, and replacing characters. Examples include substr(), toupper(), tolower(), and gsub().

  • Data Visualization Functions:
    R is renowned for its powerful data visualization capabilities. It provides built-in functions for creating various types of plots, including scatter plots, bar charts, histograms, and more. Examples include plot(), barplot(), hist(), and boxplot().

  • Control Flow Functions:
    R includes control flow functions like ifelse(), for(), while(), and switch(), which enable users to implement conditional logic and control the flow of execution in their code.

  • File Input/Output Functions:
    R offers functions to read data from files (e.g., read.csv(), read.table()) and write data to files (e.g., write.csv(), write.table()).

These are just a few examples of the various built-in functions in R. As R is an open-source language, its functionality can be further extended with the use of additional packages from the Comprehensive R Archive Network (CRAN) or other sources. Built-in functions in R significantly contribute to the language's versatility and popularity in various data analysis and statistical domains.

User-defined Functions in R

In R, user-defined functions are functions created by the users themselves to perform specific tasks or operations tailored to their needs. These functions are essential for code modularity, reusability, and organization. When you define a user-defined function, you give it a name and specify its behavior using R code.

Creating a User-defined Function in R:

To create a user-defined function in R, you use the function keyword, followed by the function name, a set of parentheses with optional arguments, and a function body enclosed in curly braces {}. The general syntax is as follows:

Components of a User-defined Function:

  • Function Name:
    A unique identifier that you give to the function, following R's naming rules and conventions. It allows you to call the function later in the code.

  • Arguments:
    Input parameters that the function takes during the function call. These parameters allow you to pass data or values to the function for processing.

  • Function Body:
    The set of instructions within curly braces {}. It contains the R code that defines the behavior of the function and the operations it performs.

  • Return Value:
    The optional return statement determines what the function will return as its output. If the return statement is omitted, the function will return the result of the last expression in the function body.

Example of a User-defined Function:

Let's create a simple user-defined function that calculates the square of a given number:

Using the User-defined Function:

After defining the function, you can call it by using its name and providing the required arguments:

User-defined functions in R empower programmers to encapsulate specific tasks, promote code reuse, and improve code readability. They are integral to effective R programming and allow users to create custom tools tailored to their data analysis and manipulation needs.

Calling a Function in R

Calling a Function with an Argument

Calling a function in R involves invoking the function and providing arguments (input parameters) that the function will use during its execution. The function will then process the provided arguments within its function body and may return a result as an output. Let's go through the steps of calling a function with an argument in detail:

1. Define the Function:

First, you need to define the function you want to call. You can use either a built-in function from an R package or create your user-defined function. For example, let's create a simple user-defined function that adds two numbers together:

2. Call the Function:

To call the function, use its name followed by parentheses. Inside the parentheses, provide the required arguments that the function expects. In our case, the add_numbers function takes two arguments x and y.

3. Pass Arguments:

When you call the function, the values you pass inside the parentheses are assigned to the corresponding function arguments (x and y in our example). The function then uses these values to perform its task.

4. Function Execution:

The function's code block (function body) is executed using the provided arguments. In our example, the add_numbers function will compute the sum of x and y (3 + 5) and store the result in the result variable.

5. Return Value:

If the function contains a return statement, it will generate a result or output. The function returns the value specified after the return statement. If there is no explicit return statement, the function will return the result of the last expression in the function body.

In our example, the add_numbers function contains a return statement that returns the sum of x and y.

6. Use the Result:

After calling the function, you can use the returned value (in our case, the sum of 3 and 5) as needed in your code.

Calling functions with arguments allows you to pass data dynamically and perform operations on different inputs, making your code more flexible and efficient. Whether using built-in functions or creating your own, calling functions with arguments is a fundamental concept in R programming.

Calling a Function without Any Argument

Calling a function in R without any arguments means invoking a function that does not require any input parameters during its execution. In other words, the function does not expect any values to be passed to it for processing. When calling such a function, you simply use its name followed by parentheses ().

Here's a detailed explanation of how to call a function without any arguments in R:

1. Define the Function:

First, you need to create a function that does not take any arguments. This is usually done using the function keyword, followed by the function name, an empty argument list (denoted by empty parentheses), and the function body enclosed in curly braces {}.

2. Calling the Function:

Once you have defined the function, you can call it by using its name followed by parentheses (). Since this function does not require any arguments, you do not need to pass any values to it.

When you call the function without any arguments, it will execute the code within its function body. In this case, it will display the "Hello, World!" message on the console.

Calling a function without arguments is useful when you have a task that doesn't depend on any external input. For example, printing a welcome message or initializing some variables can be performed in a function without arguments.

Remember that if you attempt to pass arguments to a function that does not expect any, you will encounter an error. So, always ensure the function definition and function call match in terms of the presence and number of arguments.

Calling a Function with Argument Values

Calling a function in R involves invoking the function with specific argument values to perform the desired task. When you call a function, you provide input data or values for the function's arguments, which are then used within the function's body to execute the defined operations. Here's a step-by-step guide on how to call a function with argument values in R:

1. Define the Function:

Before calling a function, you need to define it. You can create a user-defined function or use built-in functions from R packages or libraries. Let's assume we have a simple user-defined function called add_numbers, which takes two arguments x and y, and returns their sum.

2. Call the Function:

To call the function, use the function name followed by parentheses (). Inside the parentheses, provide the values for the function's arguments in the order they appear in the function definition.

In this example, we provided 5 for the x argument and 3 for the y argument. The function body then adds these two numbers together and returns the result, which is stored in the variable result.

3. Argument Order Matters:

The order of the arguments matters when calling a function. In the example above, 5 was passed as the first argument (x) and 3 as the second argument (y). If you were to switch the order, the result would be different.

4. Named Arguments:

Instead of relying on the order of the arguments, you can explicitly provide argument names when calling a function. This way, the order doesn't matter, and it can improve code readability.

Calling functions with argument values is a fundamental aspect of R programming. It allows you to perform various tasks using functions tailored to specific requirements and data. Whether you use built-in functions or create your own, mastering function calls with arguments enables efficient data analysis and manipulation in R.

Calling a Function with Default Argument

Calling a function with default arguments in R allows you to provide values for only some of the function's parameters while using default values for the rest. Default arguments are specified when defining the function and are used when a corresponding argument is not explicitly provided during the function call. This feature enhances flexibility and simplifies function calls by reducing the number of required arguments.

Creating a Function with Default Arguments:

To define a function with default arguments, you set default values for specific parameters in the function definition. Here's the general syntax:

In the function definition, you assign default values to some or all of the arguments. If an argument is not provided during the function call, it will take on its default value.

Example of a Function with Default Argument:

Let's create a function that calculates the area of a rectangle with default values for width and height:

Calling a Function with Default Arguments:

When calling a function with default arguments, you can omit the default arguments if you want to use their predefined values, or you can provide new values for them.

Example of calling the rectangle_area function:

In the first call, we didn't provide any arguments, so the function used the default values of width = 5 and height = 10. In the second call, we provided custom values for width and height, which were used instead of the defaults.

Using default arguments in R functions enhances code flexibility and makes function calls more concise, especially when some arguments are commonly used with the same values. It allows you to create versatile functions that cater to various scenarios without the need to specify all arguments every time you call the function.

R Function Examples

Example - 1: Single Input Single Output

In this example, we'll create a simple R function that takes a single input (a number) and returns a single output (the square of that number).

  • We define a function named square using the function keyword, followed by the input parameter x.
  • Inside the function body, we calculate the square of x by raising it to the power of 2 and store the result in the variable result.
  • The return(result) statement indicates that the function will return the value of the result as its output.

Example - 2: Multiple Input Multiple Output

Here's an example of a user-defined R function that takes multiple inputs and returns multiple outputs:

In this example, the function calculate_operations takes two input arguments a and b. It calculates the sum, difference, product, and division of these two numbers. The division is handled carefully to avoid division by zero. Here the result is returned as a list.

Example - 3: Using Function Inside Another Function

Suppose we have two functions: one to calculate the square of a number (square) and another to calculate the sum of squares of two numbers (sum_of_squares). We'll use the square function inside the sum_of_squares function.

Here's the code for the example:

  • We first define the square function, which takes a single argument x and returns the square of x.
  • Next, we define the sum_of_squares function, which takes two arguments a and b. Inside this function, we call the square function twice, passing a and b as arguments, respectively, to calculate their squares.
  • The sum_of_squares function then adds the squares of a and b and returns the result.
  • In the example function call, we call sum_of_squares(3, 4), which calculates the sum of squares of 3 and 4 (i.e., 3^2 + 4^2) and returns the result, which is 25.

Passing Arguments to Functions in R Programming

In R programming, passing arguments to functions is a fundamental concept that enables you to provide input data or values to the function for processing. Arguments act as placeholders that store the values passed to the function during the function call.

Two types of arguments can be passed to functions in R:

1. Positional Arguments:

  • Positional arguments are the most common type of arguments used in R functions.
  • They are matched based on their positions in the function call.
  • The order of the arguments matters, and you must pass them in the correct order as defined in the function's argument list.

Example of positional arguments:

2. Named Arguments:

  • Named arguments allow you to explicitly specify which argument corresponds to which value during the function call.
  • They provide more flexibility, as you can change the order of the arguments or omit some of them, as long as you specify their names.

Example of named arguments:

Passing arguments to functions allows you to reuse the same function for different data or values, enhancing code modularity and efficiency. Additionally, you can combine both positional and named arguments in a function call if desired.

For functions with default arguments, you can omit certain arguments if you want to use their default values. R provides great flexibility in passing arguments, making the language user-friendly and accommodating to various programming styles.

Lazy Evaluations of Function in R

  • Delayed Evaluation:
    When a function is called, the arguments provided are not immediately computed or executed. Instead, R creates a promise (a delayed computation) for each argument, capturing the expression and environment where the function is called.

  • Evaluation on Demand:
    The actual evaluation of the function arguments occurs only when the argument is used or referenced within the function body. This avoids unnecessary computations if some arguments are not used during the function execution.

  • Lazy Evaluation with Expressions:
    In R, expressions are objects representing code that can be evaluated. Function arguments are often expressions, and they are evaluated only when their values are needed. This is particularly useful when working with complex or computationally expensive expressions.

  • Pros and Cons:
    Lazy evaluation can lead to more efficient code, especially when dealing with large datasets or computationally intensive operations. However, it can also lead to unexpected behavior if side effects (such as modifying objects outside the function) occur due to delayed evaluation.

Example of Lazy Evaluation in R:

In this example, when the lazy_function is called with the argument 5 + 3, the actual addition operation (5 + 3) is not immediately performed. Instead, R creates a promise to evaluate 5 + 3 when it is needed inside the function. In this case, the expression 5 + 3 is needed to calculate the square, and then it is evaluated, resulting in "Function is called." being printed once, and the function returns 64.

Conclusion

  • Functions are essential building blocks in R programming, enabling developers to modularize code and promote code reusability and maintainability.
  • Users can create custom functions tailored to their specific needs by defining a function name, specifying arguments, writing function bodies, and, optionally, adding a return statement.
  • A typical R function consists of a function name, arguments, a function body (containing the code to be executed), and an optional return statement.
  • Functions are invoked through function calls, providing input arguments that the function processes and returns the result.
  • Functions can have default arguments assigned during function definition, allowing users to omit certain arguments during function calls, while the function uses predefined default values for them.
  • Understanding and using functions efficiently is of paramount importance in data analysis, as it is one of the primary uses of R.
  • Functions in R enable users to encapsulate complex operations, promoting code reusability and maintainability.
  • Efficiently using functions streamlines data analysis workflows, reducing code duplication and improving code readability.
  • Functions aid in modularizing data analysis tasks, making it easier to collaborate and share code with others.